If your primary key field is an identity field, DO NOT populate it when you insert it into the database. For example, given a Customer table with the following structure:
Customer
========
Customer_ID (PK, identity)
Last_Name (varchar)
First_Name (varchar)
Middle_Initial (char)
this is possible:
public int CreateCustomer(string lastName, string firstName, string middleInitial)
{
using (DataContext dc = new DataContext())
{
Customer customer = new Customer();
customer.Last_Name = lastName;
customer.First_Name = firstName;
customer.Middle_Initial = middleInitial;
dc.Customers.InsertOnSubmit(customer);
dc.SubmitChanges();
return customer.Customer_ID;
}
}
The most likely reason for your error is that you are trying to access the entity object after the DataContext in which it was created has been destroyed. Using the above example, this will produce an error similar to the one you are probably receiving:
public int CreateCustomer(string lastName, string firstName, string middleInitial)
{
using (DataContext dc = new DataContext())
{
Customer customer = new Customer();
customer.Last_Name = lastName;
customer.First_Name = firstName;
customer.Middle_Initial = middleInitial;
dc.Customers.InsertOnSubmit(customer);
dc.SubmitChanges();
}
return customer.Customer_ID; // <<-- Error occurs here
}
The reason the error occurs is due to the built-in change tracking of the LINQ engine. When the DataContext is created, any entity objects created within that DataContext are tied to that DataContext via a reference within the entity object. Once the DataContext falls out of scope, the reference is no longer valid and the contents of the entity object are no longer considered reliable to the LINQ engine.
As far as transaction handling is concerned, a transaction is created when the DataContext is created. When SubmitChanges
is called, all changes are executed within the context of the DataContext's transaction, no matter how many entities/tables are involved.