当我没有为标识列指定值时,为什么我在LINQ to SQL中获得"无法为标识列插入显式值"?

Fal*_*ick 5 c# linq-to-sql

我有一个看起来像这样的表: 替代文字

ClientID是我在表中唯一的标识列.UserID是不同表主键的FK.

这是我的Linq to SQL插入代码:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你很好奇,我有所有这些作业的原因是因为我在我的POCO域对象和linq生成的对象之间进行翻译.在此异常的情况下,它采用else语句的路径,创建一个新的客户端.

您可以看到我没有触及ClientID属性,该属性是表中的〜唯一〜标识列.

当IDENTITY_INSERT设置为OFF时,为什么我得到"无法在表'Client'中为标识列插入显式值?

如果有用,这是我的stacktrace:

System.Data.SqlClient.SqlException
未被用户代码处理Message ="当IDENTITY_INSERT设置为OFF时,无法在表'Client'中为标识列插入显式值."
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) System.Data.SqlClient.TdsParser.Run上的System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)中的System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)处的异常,布局breakConnection)(RunBehavior runBehavior,的SqlCommand cmdHandler,SqlDataReader的数据流,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader的DS,RunBehavior runBehavior,字符串resetOptionsString)在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBeh)中的returnStream,Boolean async)avior,Boolean returnStream,String方法,DbAsyncResult结果)System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult结果,String methodName,Boolean sendToPipe),位于System.Data.Linq的System.Data.SqlClient.SqlCommand.ExecuteNonQuery()处. System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表达式查询,QueryInfo)中的SqlClient.SqlProvider.Execute(表达式查询,QueryInfo queryInfo,IObjectReaderFactory factory,Object [] parentArgs,Object [] userArgs,ICompiledSubQuery [] subQueries,Object lastResult) [] queryInfos,IObjectReaderFactory factory,Object [] userArguments,ICompiledSubQuery [] subQueries)在System.Data.Linq的System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)中. System.D的System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)上的System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject项)中的ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject项)在DomainModel.Repository.Concrete.SqlClientRepository.InsertClientByUsername(String username,Client clientInfo)的System.Data.Linq.DataContext.SubmitChanges()处的ata.Linq.DataContext.SubmitChanges(ConflictMode failureMode)

w4i*_*4ik 1

从这里

尝试将“自动生成的值”设置为 false