升級到EF Core 1.1後,標識值生成只能與有符號整數屬性一起使用

asp.net-core asp.net-core-1.0 entity-framework-core

當我將“Microsoft.EntityFrameworkCore.Tools.DotNet”更新為版本“1.1.0-preview4”時 ,實體框架已停止生成遷移。

錯誤:

dotnet : System.ArgumentException: Identity value generation cannot be used      
for the property 'UID' on entity type 'SomeEntity' because the property type is   
'Guid'. Identity value generation can only be used with signed integer properties.

熱門答案

解決方法是擺脫屬性[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

[Key]
// [DatabaseGenerated(DatabaseGeneratedOption.Identity)] <-- remove this
public Guid UID { get; set; }

並更新模型構建器

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // add this:
    modelBuilder.Entity<SomeEntity>().Property(p => p.UID).ValueGeneratedOnAdd();
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因