gpt4 book ai didi

entity-framework-core - 使用 EntityFramework Core 2.2 为具有数据库生成 key 的数据做种子

转载 作者:行者123 更新时间:2023-12-04 08:37:46 25 4
gpt4 key购买 nike

我正在使用 EF Core 2.2,使用代码优先的方法。

我有我的实体类:

public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int ClientID { get; set; }
public string Name { get; set; }
}

我正在像这样播种我的数据:
var client = new Client { Name = "TestClient"};
modelBuilder.Entity<Client>().HasData(client);

但是我在尝试添加迁移时收到错误消息:

The seed entity for entity type 'Client' cannot be added because a non-zero value is required for property 'ClientID'. Consider providing a negative value to avoid collisions with non-seed data.


ClientID应该自动生成,我不想指定它。是否有解决方法或此功能尚未实现?

最佳答案

对于种子数据,您必须指定 key 。否则它不知道要确保哪个记录在那里。是documented here .

关于entity-framework-core - 使用 EntityFramework Core 2.2 为具有数据库生成 key 的数据做种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54810281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com