gpt4 book ai didi

entity-framework-4 - 使用 Guid 作为 EF4 Code First 的 PK

转载 作者:行者123 更新时间:2023-12-03 07:23:45 26 4
gpt4 key购买 nike

我有这个类和表:

public class Foo
{
public Guid Id {get;set;}
public string Name {get;set;}
}
<小时/>
create table Foo
(
id uniqueidentifier primary KEY DEFAULT (newsequentialid()),
name nvarchar(255)
)

问题是,当我尝试保存新的 foo 时,第一个与 0000-000-00 ... id 一起使用,第二个也与 0000-000-00 ... id 一起使用,所以我得到了约束异常

有人知道修复方法吗?

最佳答案

你设置了Identity StoreGeneratePattern吗?
您可以在 OnModelCreating 方法中执行此操作:

modelBuilder.Entity<Foo>().Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

或使用 DataAnnotation 属性:

public class Foo {
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id {get;set;}
public string Name {get;set;}
}

关于entity-framework-4 - 使用 Guid 作为 EF4 Code First 的 PK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5270721/

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