locked
Inserting an item / IDENTITY column RRS feed

  • Question

  • Hi,

    i'm trying to insert an item to the DB  using this code (inside a web service):

                ...
                GetNotesDataContext db = new GetNotesDataContext();
                db.Notes.InsertOnSubmit(note);
                db.SubmitChanges();
                ...

    note is an obiect that includes properties such:  id, title, body etc...

    The problem is that column id in the DB is an identity column (autoincremented).   Every time i try to insert a note I'm getting this error:

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

    How should I fix it?

    thanks in advance!

    cheers

    pete

     

    Monday, June 14, 2010 10:57 AM

Answers

  • 1. where exactly shall I fire the script provided?

    Look SET IDENTITY_INSERT (Transact-SQL)

    2. What value shall i assign to this column in my query?

    You must leave Id field(property) of entity with empty value(don't set it in code). Then you don't need to use that script.

    Monday, June 14, 2010 11:32 AM

All replies

  • Hi Pete,

    Try this DB script:

    SET IDENTITY_INSERT <put here note table name> ON
    GO

    Monday, June 14, 2010 11:13 AM
  •  Hi Sergey!

    2 questions:

    1. where exactly shall I fire the script provided?
    2. What value shall i assign to this column in my query?

    cheers

    pete

     

    Monday, June 14, 2010 11:20 AM
  • 1. where exactly shall I fire the script provided?

    Look SET IDENTITY_INSERT (Transact-SQL)

    2. What value shall i assign to this column in my query?

    You must leave Id field(property) of entity with empty value(don't set it in code). Then you don't need to use that script.

    Monday, June 14, 2010 11:32 AM