Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 先对现有数据库、实体框架编码:当identity_insert设置为OFF时,无法在表中插入identity列的显式值_C#_Sql Server_Entity Framework - Fatal编程技术网

C# 先对现有数据库、实体框架编码:当identity_insert设置为OFF时,无法在表中插入identity列的显式值

C# 先对现有数据库、实体框架编码:当identity_insert设置为OFF时,无法在表中插入identity列的显式值,c#,sql-server,entity-framework,C#,Sql Server,Entity Framework,首先,请不要将此标记为重复。我知道它已经被触摸过好几次了,但我相信我的情况是不同的。我正在做一个代码优先的方法,其中数据库基本上已经存在于一些for或其他for中 调用DbContext.SaveChanges()时,我收到以下错误:“当identity\u insert设置为OFF时,无法在表'CustomerShippingConfiguration'中为identity列插入显式值”。正如您在下面生成的SQL中所看到的,Entity Framework正在尝试将0插入到新记录的Id列中 I

首先,请不要将此标记为重复。我知道它已经被触摸过好几次了,但我相信我的情况是不同的。我正在做一个代码优先的方法,其中数据库基本上已经存在于一些for或其他for中

调用DbContext.SaveChanges()时,我收到以下错误:“当identity\u insert设置为OFF时,无法在表'CustomerShippingConfiguration'中为identity列插入显式值”。正如您在下面生成的SQL中所看到的,Entity Framework正在尝试将0插入到新记录的Id列中

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();
    }
}
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();
    }
}

我有另一个实体,它是在一个非常相似的模式,工作刚刚好设置。我在这里真是不知所措。提前感谢您的建议。

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

using System.ComponentModel.DataAnnotations.Schema;

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

        Property(a => a.Id)
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}
使用System.ComponentModel.DataAnnotations.Schema;
公共类ShippingConfigurationConfiguration:EntityTypeConfiguration
{
公共ShippingConfigurationConfiguration()
{
HasKey(t=>t.Id);
属性(a=>a.Id)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}

不是答案,但是
EntityBase
有一个未使用的通用参数有什么意义呢?老实说,它是遗留的,需要删除。它过去是有目的的,但现在不再有了。哈哈,这很公平。在这种情况下,您应该意识到,编写一个注释可能会更快地回答您的问题,并且不会像我这样愚蠢的问题:)添加
[Key]
[DatabaseGenerated(DatabaseGenerationOption.Identity)]
注释到
Id
属性。我对原始问题添加了一些编辑。问题似乎在于父实体、CustomerEntity和ShippingConfigurationEntity之间定义的关系。我已经尝试过这一点,但可能不是针对IsRequired。我认为这又产生了一个错误。让我试试,我会发回的。奇怪的是,我不必对我的其他实体这样做。这就好像EF只是假设默认情况下它是数据库生成的一样。这会生成“引用约束中的依赖属性映射到存储生成的列。列:'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);
    }
}