简体   繁体   English

实体框架代码中的种子主键在SQL Server Compact Edition中首先为0

[英]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.

相关问题 具有实体框架的SQL Server Compact Edition - SQL Server Compact Edition with Entity Framework 实体框架代码的SQL Server Compact 4.0连接字符串首先? - SQL Server Compact 4.0 connection string for Entity Framework code first? Entity Framework 4.3和SQL Server Compact Edition 3.5的更新缓慢 - Slow updates with Entity Framework 4.3 and SQL Server Compact Edition 3.5 在Entity Framework和SQL Server Compact Edition数据库中获取错误 - get Error in Entity Framework and SQL Server Compact edition database 首先删除实体框架代码中的主键 - deleting primary key in entity framework code first 违反主要密钥实体框架代码 - Violation of primary key Entity Framework Code First 如何告诉Entity Framework首先在代码中使用SQL Server Compact? - How do I tell Entity Framework to use SQL Server Compact in code first? 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? 哪个sql server版本最适合使用实体框架代码快速保存数据? - which sql server edition is the best for saving data fast using entity framework code first? 如何使用应用程序部署.Net Compact Framework和SQL Server Compact Edition - How to deploy the .Net Compact Framework and SQL Server Compact Edition with the Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM