简体   繁体   English

Entity Framework Core 尝试更新模型中标记为 Identity 的列

[英]Entity Framework Core attempting to update column marked as Identity in model

I have a database table where one of the columns is an IDENTITY column (not the primary key which is set manually).我有一个数据库表,其中一列是IDENTITY列(不是手动设置的主键)。 My EF Core model looks like this:我的 EF Core 模型如下所示:

public class Model
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    ...Other fields...

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AnotherId { get; set; }
}

Despite the annotation, when I do an update on the table I find that the SQL which is generated tries to update the IDENTITY column:尽管有注释,但当我对表进行更新时,我发现生成的 SQL 尝试更新IDENTITY列:

UPDATE [Model] SET ... [AnotherId] = @p10 ...
WHERE [Id] = @p29;
SELECT @@ROWCOUNT;

This obviously fails, causing the an exception to be thrown.这显然失败了,导致抛出异常。 I am using Entity Core 2.0 and upgrading to 2.1 is currently not an option.我正在使用 Entity Core 2.0,目前无法升级到 2.1。 Any ideas why the data annotation is being ignored?为什么数据注释被忽略的任何想法?

Try Fluent API in OnModelCreating of the Context class.Context类的OnModelCreating中尝试Fluent API Something like this:像这样的东西:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
    .Entity<Model>()
    .Property(c => c.AnotherId )
    .UseSqlServerIdentityColumn();
}

Also in EF Core 2 it should be something like this:同样在 EF Core 2 中,它应该是这样的:

modelBuilder.Property(c => c.AnotherId).UseSqlServerIdentityColumn();
modelBuilder.Property(p => p.AnotherId )
            .Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;

The attribute you likely want to use is:您可能要使用的属性是:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

as per the docs .根据文档

The solution for me in EF Core 3.1 was this:我在 EF Core 3.1 中的解决方案是:

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<YourTableName>().Property(c => c.YourPropertyName).UseIdentityColumn().ValueGeneratedOnAddOrUpdate();
    }

Without ValueGeneratedOnAddOrUpdate() I was seeing the same error as the OP when updating existing records.如果没有 ValueGeneratedOnAddOrUpdate(),我在更新现有记录时看到了与 OP 相同的错误。 It looks therefore as though UseIdentityColumn() is equivalent to calling ValueGeneratedOnAdd().因此,看起来 UseIdentityColumn() 等效于调用 ValueGeneratedOnAdd()。

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

相关问题 将 object 映射为实体框架核心中的标识列 - Mapping object as identity column in entity framework core 身份核心与实体框架6 - Identity Core with Entity Framework 6 如何使用 Entity Framework Core 配置标识列? - How to configure an Identity column using Entity Framework Core? Entity Framework Core:无法为表中的标识列插入显式值 - Entity Framework Core: Cannot insert explicit value for identity column in table MVC身份实体框架模型更新失败-“已存在对象”错误 - MVC Identity Entity Framework model update fails - “Already an object” error 实体框架并插入标识列 - Entity Framework and insert with an Identity column 当 IDENTITY_INSERT 设置为 OFF 时,无法为标识列插入显式值。 (实体框架核心) - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core) 实体与身份用户关联时,实体框架核心 3.2 更新失败 - Failure Entity Framework Core 3.2 update while entity has association with Identity User 如何在实体框架核心中使用私有属性更新 Model? - How To Update A Model With Private Properties in Entity Framework Core? 在 Entity Framework Core 中使用 Update-Database 添加新列 - Adding new column using Update-Database in Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM