none
Inserting an Entity Object (DataContext) RRS feed

  • Question

  • I posted this question in ADO.NET discussions, I think that was wrong, so I'm posting it again in this Discussion Group (sorry for that!)

    I'm trying to insert a new entity object in a table whose primary key is also a identity column. It throws an exception:

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

    The problem is that when I create the entity object to insert, I set all fields (properties) except the one corresponding to the primary key (which is an int, identity, increment 1), but the field that corresponds to the primary key is initialy set to 0 when the object is created (which I suppose should be null ??).

    The code is similar to this:

    entityObject = new EntityObject{
       name = "name"
       address = "address"
    };

    EntityObject has other property named id, which corresponds to the table primary key, int, identity column, increment 1.

    I receive the exception when doing:

    db.InsertOnSubmit(entityObject);  // db is my DataContext connected to the DataBase, EVERYTHING ELSE WORKS FINE: DELETING AND UPDATING
    db.SubmitChanges();  // <==== I get the exception here....

    I want SQLExpress to generate the key automatically.

    Could anybody tell me what to do?

    Thanks in advance...

    Rafael.

    Sunday, June 28, 2009 7:42 PM

All replies

  • I think I solved it already, I found a property in designer that says Auto-Generated Value true/false...

    ...


    Sunday, June 28, 2009 9:43 PM