标识值生成只能在升级到 EF Core 1.1 后与带符号整数属性一起使用

问题描述 投票:0回答:1

当我将“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.
asp.net-core entity-framework-core asp.net-core-1.0
1个回答
3
投票

解决方案是摆脱属性[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();
}
© www.soinside.com 2019 - 2024. All rights reserved.