繁体   English   中英

使用现有数据库和实体框架的代码优先:当IDENTITY_INSERT设置为OFF时,无法为表中的标识列插入显式值

[英]Code First With Existing Database, Entity Framework: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF

首先,请不要将此标记为重复。 我知道它已经被感动了几次,但是我相信我的情况有所不同。 我正在执行代码优先方法,其中数据库基本上已经存在于for或another中。

当我调用DbContext.SaveChanges()时,出现以下错误:“当IDENTITY_INSERT设置为OFF时,无法在表'CustomerShippingConfiguration'中为标识列插入显式值”。 从下面的生成的SQL中可以看到,Entity Framework尝试在ID列中插入0以获得新记录。

INSERT [dbo].[CustomerShippingConfiguration]([Id], [CustomerId], [AverageCartonWeight], [AverageCartonsPerPallet], [CreatedBy], [Created], [UpdatedBy], [Updated])
VALUES (@0, @1, @2, @3, @4, @5, @6, @7)

-- @0: '0' (Type = Int32)

-- @1: '119' (Type = Int32)

-- @2: '11' (Type = Decimal, Precision = 18, Scale = 2)

-- @3: '11' (Type = Int32)

-- @4: '616' (Type = Int32)

-- @5: '8/9/2016 10:09:08 AM' (Type = DateTime)

-- @6: '616' (Type = Int32)

-- @7: '8/9/2016 10:09:08 AM' (Type = DateTime)

此Id列的类型为INT,并设置为身份列,主键和自动增量。

身份列配置

标识列规范

表架构

实体模型如下所示:

public class ShippingConfigurationEntity : EntityBase<ShippingConfigurationEntity>, IEntity
{
      public int CustomerId { get; set; }

      public virtual CustomerEntity Customer { get; set; }

      public decimal? AverageCartonWeight { get; set; }

      public int? AverageCartonsPerPallet { get; set; }

      public virtual ICollection<ShippingAddressEntity> Addresses { get; set; }

      public ShippingConfigurationEntity()
      {
          Addresses = new List<ShippingAddressEntity>();
      }
}

public abstract class EntityBase<T>
{
      public virtual int Id { get; set; }

      public virtual int CreatedBy { get; set; }

      public virtual DateTime Created { get; set; }

      public virtual int UpdatedBy { get; set; }

      public virtual DateTime Updated { get; set; }
}

实体的配置如下所示:

public class ShippingConfigurationConfiguration : EntityTypeConfiguration<ShippingConfigurationEntity>
{
    public ShippingConfigurationConfiguration()
    {
        HasKey(t => t.Id);

        Property(t => t.CustomerId).IsRequired();
        Property(t => t.AverageCartonsPerPallet).IsOptional();
        Property(t => t.AverageCartonWeight).IsOptional();
        Property(t => t.CreatedBy).IsRequired();
        Property(t => t.Created).IsRequired();
        Property(t => t.UpdatedBy).IsRequired();
        Property(t => t.Updated).IsRequired();

        ToTable("CustomerShippingConfiguration");

        HasMany(x => x.Addresses).WithRequired(x => x.ShippingConfiguration).HasForeignKey(x => x.ShippingConfigurationId).WillCascadeOnDelete();
    }
}

有一个名为Customer的父实体,其外观类似于以下内容:

public class CustomerEntity : EntityBase<CustomerEntity>, IEntity
{
    public int LocationID { get; set; }        

    public virtual ShippingConfigurationEntity ShippingConfiguration { get; set; }
}

配置如下所示:

class CustomerConfiguration : EntityTypeConfiguration<CustomerEntity>
{
    public CustomerConfiguration()
    {
        HasKey(t => t.Id);

        Property(t => t.LocationID).IsRequired();

        Ignore(t => t.Created);
        Ignore(t => t.CreatedBy);
        Ignore(t => t.Updated);
        Ignore(t => t.UpdatedBy);

        ToTable("Customer");

        Property(t => t.Id).HasColumnName("ID");
        Property(t => t.LocationID).HasColumnName("LOCATION_ID");

        HasOptional(x => x.ShippingConfiguration).WithRequired(x => x.Customer).WillCascadeOnDelete();
    }
}

如果删除CustomerEntity和ShippingConfiguration Entity之间的关系,则在ShippingConfiguration的Id属性中使用以下内容时,一切都将变为:

Property(t => t.Id).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

我有另一个实体,它以非常相似的模式设置,效果很好。 我真的很茫然。 预先感谢您的任何建议。

您需要告诉您的配置,该数据库将处理密钥生成。

using System.ComponentModel.DataAnnotations.Schema;

public class ShippingConfigurationConfiguration : EntityTypeConfiguration<ShippingConfigurationEntity>
{
    public ShippingConfigurationConfiguration()
    {
        HasKey(t => t.Id);

        Property(a => a.Id)
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

暂无
暂无

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

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