我正在使用Microsoft SQL Server 2008 R2,并且具有三个相关表:
转换碳水化合物,国家和州省

使用EF Power Tools,我对数据库(http://msdn.microsoft.com/en-us/data/jj593170.aspx)进行了反向工程,并尝试使用Entity Framework将记录插入ConvertCarb表中。

但是,该程序在尝试保存更改时抛出以下错误:


  当IDENTITY_INSERT设置为OFF时,无法为表'ConvertCarb'中的标识列插入显式值。


这是运行时代码:

var convertCarb = new ConvertCarb();
convertCarb.CountryID = 150;
db.ConvertCarbs.Add(convertCarb);
db.SaveChanges();


这是域模型中的类:

public partial class ConvertCarb
{
    public int ConvertCarbID { get; set; }
    public Nullable<int> CountryID { get; set; }
    public virtual Country Country { get; set; }
}

public partial class Country
{
    public Country()
    {
        this.ConvertCarbs = new List<ConvertCarb>();
        this.Offices = new List<Office>();
    }

    public int CountryID { get; set; }
    public string CountryName { get; set; }
    public virtual ICollection<ConvertCarb> ConvertCarbs { get; set; }
}

public partial class StateProvince
{
    public StateProvince()
    {
        this.Offices = new List<Office>();
    }

    public int StateProvID { get; set; }
    public string StateProvAbbr { get; set; }
    public virtual ConvertCarb ConvertCarb { get; set; }
    public virtual ICollection<Office> Offices { get; set; }
}


进行此插入应该没有问题,因为ConvertCarbID是“身份列”,并且我已经通过直接在数据库上运行tsql插入来验证了这一点。

这是数据库中表的创建代码:

/****** Object:  Table [dbo].[ConvertCarb]    Script Date: 04/11/2014 03:04:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ConvertCarb](
    [ConvertCarbID] [int] IDENTITY(1,1) NOT NULL,
    [CountryID] [int] NULL,
    [StateProvID] [int] NULL,
    [KWH_FT2] [float] NULL,
    [G_KWH] [decimal](18, 4) NULL,
    [NatGas_GJ_M2] [float] NULL,
    [FuelOil_GJ_M2] [float] NULL,
 CONSTRAINT [PK_ConvertCarb] PRIMARY KEY CLUSTERED
(
    [ConvertCarbID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[ConvertCarb]  WITH NOCHECK ADD  CONSTRAINT [FK_ConvertCarb_Countries] FOREIGN KEY([CountryID])
REFERENCES [dbo].[Countries] ([CountryID])
GO

ALTER TABLE [dbo].[ConvertCarb] NOCHECK CONSTRAINT [FK_ConvertCarb_Countries]
GO

ALTER TABLE [dbo].[ConvertCarb]  WITH NOCHECK ADD  CONSTRAINT [FK_ConvertCarb_StateProvinces] FOREIGN KEY([ConvertCarbID])
REFERENCES [dbo].[StateProvinces] ([StateProvID])
GO

ALTER TABLE [dbo].[ConvertCarb] NOCHECK CONSTRAINT [FK_ConvertCarb_StateProvinces]
GO


/****** Object:  Table [dbo].[Countries]    Script Date: 04/11/2014 03:04:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Countries](
    [CountryID] [int] IDENTITY(1,1) NOT NULL,
    [CountryName] [varchar](255) NULL,
 CONSTRAINT [PK_Countries] PRIMARY KEY CLUSTERED
(
    [CountryID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO




/****** Object:  Table [dbo].[StateProvinces]    Script Date: 04/11/2014 03:05:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[StateProvinces](
    [StateProvID] [int] IDENTITY(1,1) NOT NULL,
    [StateProvAbbr] [varchar](2) NULL,
 CONSTRAINT [PK_StateProvinces] PRIMARY KEY CLUSTERED
(
    [StateProvID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


以下是EF工具生成的映射类:

public class ConvertCarbMap : EntityTypeConfiguration<ConvertCarb>
{
    public ConvertCarbMap()
    {
        // Primary Key
        this.HasKey(t => t.ConvertCarbID);

        // Properties
        // Table & Column Mappings
        this.ToTable("ConvertCarb");
        this.Property(t => t.ConvertCarbID).HasColumnName("ConvertCarbID");
        this.Property(t => t.CountryID).HasColumnName("CountryID");
        this.Property(t => t.StateProvID).HasColumnName("StateProvID");
        this.Property(t => t.KWH_FT2).HasColumnName("KWH_FT2");
        this.Property(t => t.G_KWH).HasColumnName("G_KWH");
        this.Property(t => t.NatGas_GJ_M2).HasColumnName("NatGas_GJ_M2");
        this.Property(t => t.FuelOil_GJ_M2).HasColumnName("FuelOil_GJ_M2");

        // Relationships
        this.HasOptional(t => t.Country)
            .WithMany(t => t.ConvertCarbs)
            .HasForeignKey(d => d.CountryID);
        this.HasRequired(t => t.StateProvince)
            .WithOptional(t => t.ConvertCarb);

    }
}





public class CountryMap : EntityTypeConfiguration<Country>
{
    public CountryMap()
    {
        // Primary Key
        this.HasKey(t => t.CountryID);

        // Properties
        this.Property(t => t.CountryName)
            .HasMaxLength(255);

        // Table & Column Mappings
        this.ToTable("Countries");
        this.Property(t => t.CountryID).HasColumnName("CountryID");
        this.Property(t => t.CountryName).HasColumnName("CountryName");
    }
}


public class StateProvinceMap : EntityTypeConfiguration<StateProvince>
{
    public StateProvinceMap()
    {
        // Primary Key
        this.HasKey(t => t.StateProvID);

        // Properties
        this.Property(t => t.StateProvAbbr)
            .HasMaxLength(2);

        // Table & Column Mappings
        this.ToTable("StateProvinces");
        this.Property(t => t.StateProvID).HasColumnName("StateProvID");
        this.Property(t => t.StateProvAbbr).HasColumnName("StateProvAbbr");
    }
}

最佳答案

此关系映射...

this.HasRequired(t => t.StateProvince)
    .WithOptional(t => t.ConvertCarb);


...表示在共享主键一对一关系中,StateProvince是主体,ConvertCarb是从属。在那种情况下,EF假定只有主体可以具有身份主键,而不具有从属ConvertCarb(因为从属必须始终具有与主体相同的PK值)。基本上,这种关系会禁用ConvertCarb实体的身份约定。结果,EF将ConvertCarb实体的PK值(无论是手动提供还是默认提供)发送到数据库(ConvertCarbID列是INSERT语句的一部分),这会引发数据库异常,因为ConvertCarbID列标记为身份。

但是我不知道为什么EF从数据库中选择了一对一的关系,因为我在数据库脚本中看不到StateProvince的FK约束。还是后来将约束添加到数据库了?

07-24 09:51