Linq to SQL: Why am I getting IDENTITY_INSERT errors?

前端 未结 7 636
长发绾君心
长发绾君心 2020-12-17 02:49

I\'m using Linq to SQL. I have a DataContext against which I am .SubmitChanges()\'ing. There is an error inserting the identity field:

Cannot insert explicit value for         


        
相关标签:
7条回答
  • 2020-12-17 03:21

    Ensure the property Auto Generated Value in your dbml for that column is set to true. If not, LINQ will attempt to default the value based on the type.

    0 讨论(0)
  • 2020-12-17 03:23

    my case, I forgot to update my dbml file after setting Id column to identity.

    0 讨论(0)
  • 2020-12-17 03:24

    seems like your ID is being assigned somewhere (even if just defaulting to 0) - would you care for posting some of your LINQ code?

    0 讨论(0)
  • 2020-12-17 03:26

    I was having the exact same problem.

    My solution was to manually make the identity INT column into a nullable INT? column in the DBML designer, not in the actual table of the DB of course. Also set the DbType to NULL instead of NOT NULL. Like so:

    [Column(Storage="_TestID", AutoSync=AutoSync.OnInsert, DbType="Int NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] 
    public int? TestID
    

    This works with all DB operations. When inserting simply set the identity column to NULL rather than to 0.

    0 讨论(0)
  • 2020-12-17 03:33

    Is your code setting the ID value explicitely to 0? (instead of leaving it untouched).

    Update 1: As you posted on the updated version, linq2sql is clearly passing the value to the db. Here is one I haven't had any trouble with:

    [Column(Storage="_CustomerID", AutoSync=AutoSync.Always, DbType="Int NOT NULL IDENTITY", IsDbGenerated=true)]
    public int CustomerID 
    

    I just saw another one, and it has the same exact definition of the one you are using.

    [Column(Storage="_TestID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int TestID
    

    Update 2: Regarding updates, you are not supposed to do InsertOnSubmit for that. Just update the values and call .SubmitChanges (should be throwing an exception). On the insert it is really weird, as the property attributes you posted seems to be correct, so the Insert method linq2sql generates should be correct as well. I would try, re-adding the table on the designer again and verifying all the properties are correct.

    Note that the generated insert method should look like (with a matchingRig_Insert):

    private void InsertRig(Rig obj)
    {
        System.Nullable<int> p1 = obj.Id;
    

    this.Rig_Insert(/* bunch of values */, ref p1); obj.Id = p1.GetValueOrDefault(); }

    0 讨论(0)
  • 2020-12-17 03:33

    If you are supplying the value of the IDENTITY column, you must add

    SET IDENTITY_INSERT ON
    

    before your SQL Statements, and

    SET IDENTITY_INSERT OFF 
    

    after to turn it off. That goes for ANY raw SQL. If you used LINQ to create an object like:

    var customer = new LinqContext.Customer;
    customer.FirstName = "Bob";
    customer.LastName = "Smith";
    
    LinqContent.Customer.Add(customer);
    LinqContext.SubmitChanges();
    

    I believe that will work, forgive me if I have classes wrong. You get the basic idea.

    Edit: Oops.. Sorry, didn't read the entire question, I take it back, tired and fatigued from work...

    0 讨论(0)
提交回复
热议问题