简体   繁体   English

EFCore 3 - 保存项目抛出 sql 异常“无法更新标识列”

[英]EFCore 3 - Saving Item throw sql exception 'cannot update identity column'

i'm working on Entity Framework Core 3.0 in a .net Core 3.0 but when i trying to update this item (only this class, in a db first model) it gives me this error我正在 .net Core 3.0 中的 Entity Framework Core 3.0 上工作,但是当我尝试更新这个项目时(只有这个类,在 db first 模型中)它给了我这个错误

Sql Exception. Sql 异常。 cannot update identity column 'IDNRR'无法更新标识列“IDNRR”

i just get an item from X table and then save it with context.Update command我只是从 X 表中获取一个项目,然后使用 context.Update 命令保存它

Unit test with exception单元测试异常

var c = _dbContext.TblWoTaskExtServices.SingleOrDefault(x => x.Idnrr == 15273778);
_dbContext.Update(c);
_dbContext.SaveChanges(); -> execute this give me the sql exception

单元测试异常

class definition类定义

modelBuilder.Entity<TblWoTaskExtServices>(entity =>
{
    entity.HasKey(e => new { e.ExternalServiceCode, e.IdWotask });

    entity.ToTable("tbl_WO_TASK_EXT_SERVICES");

    entity.HasIndex(e => e.Idnrr)
        .HasName("IX_tbl_WO_TASK_EXT_SERVICES")
        .IsUnique();

    entity.Property(e => e.ExternalServiceCode)
        .HasMaxLength(255)
        .IsUnicode(false);

    entity.Property(e => e.IdWotask).HasColumnName("IdWOTask");

    entity.Property(e => e.DateIns).HasColumnType("smalldatetime");

    entity.Property(e => e.DateMod).HasColumnType("smalldatetime");

    entity.Property(e => e.ExternalServiceDescription).HasColumnType("text");

    entity.Property(e => e.Idnrr)
        .HasColumnName("IDNRR")
        .ValueGeneratedOnAdd();

    entity.Property(e => e.Notes).HasColumnType("text");

    entity.Property(e => e.TotalCost).HasColumnType("decimal(18, 2)");

    entity.Property(e => e.TotalCostFinal).HasColumnType("decimal(18, 2)");

    entity.HasOne(d => d.IdCompanyNavigation)
        .WithMany(p => p.TblWoTaskExtServices)
        .HasPrincipalKey(p => p.Idnrr)
        .HasForeignKey(d => d.IdCompany)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("FK_tbl_WO_TASK_EXT_SERVICES_tbl_COMPANIES");

    entity.HasOne(d => d.IdCostClassNavigation)
        .WithMany(p => p.TblWoTaskExtServices)
        .HasPrincipalKey(p => p.Idnrr)
        .HasForeignKey(d => d.IdCostClass)
        .HasConstraintName("FK_tbl_WO_TASK_EXT_SERVICES_tbl_COST_CLASSES");

    entity.HasOne(d => d.IdCurrencyNavigation)
        .WithMany(p => p.TblWoTaskExtServices)
        .HasPrincipalKey(p => p.Idnrr)
        .HasForeignKey(d => d.IdCurrency)
        .HasConstraintName("FK_tbl_WO_TASK_EXT_SERVICES_tbl_Currencies");
    });
}

Solved without use of Update() method不使用 Update() 方法解决

as @metacube suggest正如@metacube 建议的那样

Also, you have not changed the returned entity but used DbContext.Update on it, which changed entity state to "Modified".此外,您没有更改返回的实体,而是对其使用了 DbContext.Update,这将实体状态更改为“已修改”。 I suggest simply changing the needed properties of the tracked entity and then call SaveChanges我建议简单地更改被跟踪实体的所需属性,然后调用 SaveChanges

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM