Entity Framework Throwing Cannot Insert Explicit Value for Identity Column In Table ... When IDENTITY_INSERT is set to OFF Error

c# entity-framework sql-server

Question

I have three pertinent tables in Microsoft SQL Server 2008 R2, which I'm using: Converting StateProvinces, Countries, and Carbs

I've reverse-engineered the database (http://msdn.microsoft.com/en-us/data/jj593170.aspx) using the EF Power Tools, and now I'm attempting to use Entity Framework to enter a record into the ConvertCarb table.

But when attempting to save the modifications, the application throws the following error:

Cannot insert explicit value for identity column in table 'ConvertCarb' when IDENTITY_INSERT is set to OFF.

The run-time code is as follows:

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

The classes from the domain model are as follows:

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

Since the ConvertCarbID is an Identity Column, and I have confirmed this by doing a tsql insert on the database directly, there shouldn't be any issues with performing this insert.

The creation code for the database tables is provided here:

/****** 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

The mapping classes produced by the EF tool are as follows:

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");
    }
}
1
1
8/28/2015 8:04:24 PM

Accepted Answer

ZZZ_tmp
6
4/10/2014 8:45:05 PM

Popular Answer

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

    // Properties
    this.HasKey(t => t.ConvertCarbID)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

    // 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);

}
}

Employ the DatabaseGeneratedOption. HasDatabaseGeneratedOption's identity.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow