1

我有这样的服务方法:

[OperationContract]
public void CreateIcd9(int icd9ID, string code, string description, string specialty)
{          
    var count = from icd in clogicEntities.ICD9
                where icd.ID == icd9ID
                select icd;

    if (count.Count() >= 1)
    {
        var icd9 = clogicEntities.ICD9.Where(icd => (icd.ID == icd9ID)).SingleOrDefault();
        if (icd9 != null)
        {
            icd9.CODE = code;
            icd9.DESCRIPTION = description;
            icd9.SPECIALTY = specialty;
        }
    }
    else
    {
         ICD9 icdNew =  new ICD9()
         {
             ID = icd9ID,
             CODE = code,
             DESCRIPTION = description,
             SPECIALTY = specialty
         };                

        // Insert Icd9
        clogicEntities.ICD9.AddObject(icdNew);
    }

    clogicEntities.SaveChanges();
}

直到今天一切都很好,当我调用这个方法时我得到了一个异常,icd9ID = 1992401733 我得到了这个异常:

An error occurred while updating the entries. 
See the inner exception for details. ----> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'ID', table '1224-CLOGIC.dbo.ICD9'; column does not allow nulls. INSERT fails.
The statement has been terminated

我尝试重现此问题但没有运气,我想知道这是 MAX Int 问题:2147483647 - MAX Int 1992401733 - 我的价值请帮助我。

4

1 回答 1

0

我发现我的问题是 EF 上的 ICD9 表,ID(pk) Identity(1,1) 但在 SQL ICD9 表上 - ID(pk) Identity = false 所以当

clogicEntities.ICD9.AddObject(icdNew);

它只是将没有 ID 的 3 个值(代码、描述、专业)传递给 SQL。我将 0 int 值传递给服务的原因是我在更改参数后忘记了更新服务引用。

问候。

于 2012-09-16T06:32:36.980 回答