单独的标识列 CodeFirst mvc
Separate identity column CodeFirst mvc
我正在使用 CodeFirst 方法开发 mvc5 应用程序。这是模态 class
public class Feeder
{
[Key]
public int FeederId { get; set; }
public string FeederName { get; set; }
public string FeederCode { get; set; }
public DateTime StatusChangeDate { get; set; }
public int CreateBy { get; set; }
public DateTime CreatedDate { get; set; }
public int EditBy { get; set; }
public DateTime EditDate { get; set; }
}
这里 'FeederCode' 我想生成一个字符串,该字符串对每条记录递增。字符串应该是这样的。
BMK15FEDR00001
这里'BMK'和'FEDR'是固定的。但是 15(2015 年的最后 2 位数字)会根据年份发生变化。由于这是 2015 年,它应该是 15,对于明年的记录,它应该是 16。'00001' 应该为每条记录增加 00002、00003....
我搜索过这个但找不到正确的方法。所有帮助表示赞赏。谢谢!
为什么不将其设为计算值?
你说 BMK
和 FEDR
是常数,你可以从 CreatedDate
中提取年份,就最后一部分而言,你的 FeederId
已经设置为 auto-gen默认情况下,SQL 正在为您制作。
现在您的计算值应保存为:
`"BMK" + CreatedDate.ToString("yy") + "FEDR" + FeederId;
这就是您保存在 FeederCode
值中的内容。
希望这就是你的意思。
如果您还希望每年的代码再次从 1 开始,您应该设置一个复合主键,另一个值为年份。您还可以重置 Identity 值,您可以阅读 here 它是如何完成的(c 部分)。
关于id值的更新:
USE AdventureWorks2012;
GO
DBCC CHECKIDENT ('Person.AddressType', RESEED, 10);
GO
详见附件link:
The following example forces the current identity value in the AddressTypeID column in the AddressType table to a value of 10. Because the table has existing rows, the next row inserted will use 11 as the value, that is, the new current increment value defined for the column value plus 1.
如果这对您不重要,我建议您避免使用它。如果是,您可以根据需要使用它。实现这一点的最佳方法是触发一个 SQL 事件,每年都会重置自动标识符。
我正在使用 CodeFirst 方法开发 mvc5 应用程序。这是模态 class
public class Feeder
{
[Key]
public int FeederId { get; set; }
public string FeederName { get; set; }
public string FeederCode { get; set; }
public DateTime StatusChangeDate { get; set; }
public int CreateBy { get; set; }
public DateTime CreatedDate { get; set; }
public int EditBy { get; set; }
public DateTime EditDate { get; set; }
}
这里 'FeederCode' 我想生成一个字符串,该字符串对每条记录递增。字符串应该是这样的。
BMK15FEDR00001
这里'BMK'和'FEDR'是固定的。但是 15(2015 年的最后 2 位数字)会根据年份发生变化。由于这是 2015 年,它应该是 15,对于明年的记录,它应该是 16。'00001' 应该为每条记录增加 00002、00003....
我搜索过这个但找不到正确的方法。所有帮助表示赞赏。谢谢!
为什么不将其设为计算值?
你说 BMK
和 FEDR
是常数,你可以从 CreatedDate
中提取年份,就最后一部分而言,你的 FeederId
已经设置为 auto-gen默认情况下,SQL 正在为您制作。
现在您的计算值应保存为:
`"BMK" + CreatedDate.ToString("yy") + "FEDR" + FeederId;
这就是您保存在 FeederCode
值中的内容。
希望这就是你的意思。
如果您还希望每年的代码再次从 1 开始,您应该设置一个复合主键,另一个值为年份。您还可以重置 Identity 值,您可以阅读 here 它是如何完成的(c 部分)。
关于id值的更新:
USE AdventureWorks2012;
GO
DBCC CHECKIDENT ('Person.AddressType', RESEED, 10);
GO
详见附件link:
The following example forces the current identity value in the AddressTypeID column in the AddressType table to a value of 10. Because the table has existing rows, the next row inserted will use 11 as the value, that is, the new current increment value defined for the column value plus 1.
如果这对您不重要,我建议您避免使用它。如果是,您可以根据需要使用它。实现这一点的最佳方法是触发一个 SQL 事件,每年都会重置自动标识符。