cannot update identity column in Entity Framework Core

前端 未结 7 679
感情败类
感情败类 2021-01-04 00:52

I\'ve added a separate Identification to the AspNetUsers table called NumericId that will serve along with the GUID like ID that ASP has for default.

I\'ve added the

相关标签:
7条回答
  • 2021-01-04 01:02

    Per the discussion on GitHub surrounding this issue, for EF Core 2.0 we needed to use both lines suggested in other posts.

    for Entity framework core 2.0 , The "IsReadOnlyAfterSave" property is deprecated. Use following:

    builder.Property(p => p.Id)
        .UseSqlServerIdentityColumn();
    
    builder.Property(p => p.Id)
        .Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;
    
    0 讨论(0)
  • 2021-01-04 01:02

    Solution for asp.net core 3.1

    modelBuilder.Entity<Type>().Property(u => u.Property).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);
    
    0 讨论(0)
  • 2021-01-04 01:10

    If you are using EF Core 2.1, you can try that.

    builder.Property(e => e.ColumnName).Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;
    

    It worked for me.

    IsReadOnlyAfterSave is deprecated.

    0 讨论(0)
  • 2021-01-04 01:13

    This line solved my problem. Available from 1.1.1

    modelBuilder.Entity<ApplicationUser>().Property(u => u.NumberId).Metadata.IsReadOnlyAfterSave = true;
    
    0 讨论(0)
  • 2021-01-04 01:25

    This was a bug with EF Core 1.0. See EF trying to Insert into Identity field.

    EF Core 1.2 has marked the issue as fixed, but the workaround without updating is to use

    modelBuilder.Entity<Type>().Property(u => u.Property).UseSqlServerIdentityColumn();
    
    0 讨论(0)
  • 2021-01-04 01:25

    Using 3.1, the key is to make sure the identity value of the object you are updating matches the value of the record in the database. So if your object's ID is 0 but the id of the corresponding row in the database is 99, make sure the object's ID value is set to 99 before you try to save it.

    0 讨论(0)
提交回复
热议问题