I have a table that looks like this:
ClientID is the only identity column I have in the table. UserID is a FK to a different tables primary key.
Here is my Linq to SQL insert code:
public void InsertClientByUsername(string username, Entities.Client clientInfo)
{
using (LinqModelDataContext db = new LinqModelDataContext())
{
var existingClient = (from client in db.Clients
join ext_usr in db.User_Extendeds on client.UserID equals ext_usr.FriendlyUserID
join asp_usr in db.aspnet_Users on ext_usr.UserID equals asp_usr.UserId
where asp_usr.UserName.ToLower().Equals(username)
select client).SingleOrDefault();
if (existingClient != null)
{
existingClient.Address1 = clientInfo.Address1;
existingClient.Address2 = clientInfo.Address2;
existingClient.City = clientInfo.City;
existingClient.CompanyName = clientInfo.CompanyName;
existingClient.CountryID = clientInfo.CountryID;
existingClient.FaxNumber = clientInfo.Fax;
existingClient.FirstName = clientInfo.FirstName;
existingClient.LastName = clientInfo.LastName;
existingClient.MailingAttention = clientInfo.Attention;
existingClient.PhoneNumber = clientInfo.PhoneNumber;
existingClient.StateID = clientInfo.StateID;
existingClient.ZipCode = clientInfo.Zip;
}
else
{
int userID = (from ext_usr in db.User_Extendeds
join asp_usr in db.aspnet_Users on ext_usr.UserID equals asp_usr.UserId
where asp_usr.UserName.ToLower().Equals(username)
select ext_usr.FriendlyUserID).SingleOrDefault();
Client newClient = new Client();
newClient.UserID = userID;
newClient.Address1 = clientInfo.Address1;
newClient.Address2 = clientInfo.Address2;
newClient.City = clientInfo.City;
newClient.CompanyName = clientInfo.CompanyName;
newClient.CountryID = clientInfo.CountryID;
newClient.FaxNumber = clientInfo.Fax;
newClient.FirstName = clientInfo.FirstName;
newClient.LastName = clientInfo.LastName;
newClient.MailingAttention = clientInfo.Attention;
newClient.PhoneNumber = clientInfo.PhoneNumber;
newClient.StateID = clientInfo.StateID;
newClient.ZipCode = clientInfo.Zip;
db.Clients.InsertOnSubmit(newClient);
}
db.SubmitChanges();
}
}
In case you are curious, the reason I have all those assignments is because I'm translating between my POCO domain objects and the linq generated objects. In the case of this exception, it is taking the path of the else statement, creating a new client.
You can see that I'm NOT touching the ClientID property which is the ~only~ identity column in the table.
Why am I getting the "Cannot insert explicit value for identity column in table 'Client' when IDENTITY_INSERT is set to OFF?
In case it is useful, here is my stacktrace:
System.Data.SqlClient.SqlException was unhandled by user code
Message="Cannot insert explicit value for identity column in table 'Client' when IDENTITY_INSERT is set to OFF."
Source=".Net SqlClient Data Provider" ErrorCode=-2146232060 Class=16
LineNumber=1 Number=544
Procedure=""
Server="192.168.168.190" State=1
StackTrace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item) at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item) at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges() at DomainModel.Repository.Concrete.SqlClientRepository.InsertClientByUsername(String username, Client clientInfo)