Identity Inserts

Posts   
 
    
MWHITEHEAD
User
Posts: 8
Joined: 18-Sep-2007
# Posted on: 27-Oct-2007 17:49:21   

Borrowed bits from various threads and tried this code..

Transaction trx = new Transaction(IsolationLevel.ReadUncommitted, "MakeSomeItems");

// We need to be allowed to insert into IDENTITY field in Item
IDbCommand cmd = trx.ConnectionToUse.CreateCommand();
cmd.Transaction = trx.PhysicalTransaction;
cmd.CommandText = "set IDENTITY_INSERT Class on";
cmd.ExecuteNonQuery();

ClassEntity ce = new ClassEntity();
ce.Fields["ClassId"].ForcedCurrentValueWrite(234);
ce.AcadDataset = "2007";
ce.SubjectId = 5;
trx.Add(ce);    
ce.Save();

cmd = trx.ConnectionToUse.CreateCommand();
cmd.Transaction = trx.PhysicalTransaction;
cmd.CommandText = "set IDENTITY_INSERT Class off";
cmd.ExecuteNonQuery();

//We're done
trx.Commit();

I get an error because the PK (ClassId has no value) and I thought ForcedCurrentValueWrite would do it? Any thoughts?

Mark

Posts: 254
Joined: 16-Nov-2006
# Posted on: 27-Oct-2007 22:18:04   

This is covered in this thread http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=9003

However I'm very interested to know why you don't want to use the generated identifier values in this case?

MWHITEHEAD
User
Posts: 8
Joined: 18-Sep-2007
# Posted on: 28-Oct-2007 13:07:13   

Thanks for the thread.

The reason for wanting to do identity inserts is that in a system that imports a lot of data from different sources on a regular basis, keeping the various systems in sync by using the same PKs was just one solution.

More likely we will use the generated values and simply record the foreign system PK as a secondary key used to handle the import/export - a cleaner solution.