[英]Seed primary key in Entity Framework code first to 0 in SQL Server Compact Edition
I have this Entity Framework code first class: 我有这个实体框架代码第一类:
public class ClientEvent
{
[Key]
public int? EventCode { get; set; }
public string Description { get; set; }
}
With this seed function for SQL Server Compact Edition: 使用SQL Server Compact Edition的此种子功能:
protected override void Seed(ClientEventsContext context)
{
var clientEvent = new ClientEvent
{
EventCode = 0,
Description = "Test"
};
context.Events.Add(clientEvent);
base.Seed(context);
}
When I check the database table for ClientEvent after it runs, I see this entry: 当我在运行后检查ClientEvent的数据库表时,我看到这个条目:
EventCode | Description
---------------------------
1 | Test
Notice how the EventCode is 1? 注意EventCode是如何1的? I would expect it to be 0. How can I seed this primary key to start at 0?
我希望它是0.我怎样才能将这个主键从0开始? I have tried using the code above, but even utilizing 0 itself sets the first entry to 1.
我已尝试使用上面的代码,但即使使用0本身也将第一个条目设置为1。
There is another question similar to this which works for SQL Server, but not SQL Server Compact Edition . 还有一个与此类似的问题适用于SQL Server,但不适用于SQL Server Compact Edition 。 I have tried using its suggested answer to set the initializer to 0:
我尝试使用其建议的答案将初始化程序设置为0:
context.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('ClientEvents', RESEED, 0)");
The problem with this is: 这个问题是:
System.Data.SqlServerCe.SqlCeException: There was an error parsing the query.
System.Data.SqlServerCe.SqlCeException:解析查询时出错。 [ Token line number = 1,Token line offset = 1,Token in error = DBCC ]
[令牌行号= 1,令牌行偏移= 1,令牌错误= DBCC]
Is there not a standard way of doing this with Entity Framework code first, irrelevant of the type of database being used? 是否首先使用Entity Framework代码执行此操作的标准方法,与使用的数据库类型无关?
您可以将此语句与SQL Server Compact一起使用:
ALTER TABLE [MyTable] ALTER COLUMN [Id] IDENTITY (0,1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.