向表中添加时出现Null异常

本文关键字:Null 异常 添加 | 更新日期: 2023-09-27 17:54:38

我正在从一个web表单收集数据,代码如下:

secondContact.FirstName = txtFirstNameContact2.Text;
secondContact.LastName = txtlastNameContact2.Text;
secondContact.EmailAddress = txtEmailAddressContact2.Text;
secondContact.PhoneNumber = txtPhone1Contact2.Text + txtPhone2Contact2.Text + txtPhone3Contact2.Text;
if (!string.IsNullOrEmpty(rblContactTypeContact2.SelectedValue))
{
    secondContact.SecondaryContactTypeID = Convert.ToInt32(rblContactTypeContact2.SelectedValue);
}
secondContact.ContactPosition = 2;
secondContact.ProspectID = prospect.Id;

将代码发送到存储库以与下面的代码一起保存(它捕获了前景)。Id成功):

if (prospect.Id != 0)
   {
    if (!string.IsNullOrEmpty(secondContact.FirstName) || !string.IsNullOrEmpty(secondContact.LastName) || !string.IsNullOrEmpty(secondContact.EmailAddress) || !string.IsNullOrEmpty(secondContact.PhoneNumber))
    {
        this.repository.SaveAltContact(prospect.Id, 2, secondContact);
    }
}

存储库尝试保存数据:

public void SaveAltContact(int prospectID, int contactPosition, SecondaryContact contact)
{
    using (var context = new CoyleHomeBuyerEntities())
    {
        SecondaryContact currentAltContact = new Model.SecondaryContact();
        currentAltContact = (from sec in context.SecondaryContact
                             where sec.ContactPosition == contactPosition
                             where sec.ProspectID == prospectID
                             select sec).FirstOrDefault();
        if (currentAltContact == null)
        {
            context.AddToSecondaryContact(currentAltContact);
        }
        currentAltContact.FirstName = contact.FirstName;
        currentAltContact.LastName = contact.LastName;
        currentAltContact.EmailAddress = contact.EmailAddress;
        currentAltContact.PhoneNumber = contact.PhoneNumber;
        currentAltContact.SecondaryContactTypeID = contact.SecondaryContactTypeID;
        currentAltContact.ProspectID = prospectID;
        currentAltContact.ContactPosition = contactPosition;
        context.SaveChanges();
    }
}

它总是在这行之后失败,并出现Null异常:

context.AddToSecondaryContact(currentAltContact);

我很困惑,因为我期望上面的查询在此时返回NULL值。设置SecondaryContactID为主键并自动递增

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: entity

有人能指出我在正确的方向,为什么这是失败时,试图添加一个新的行数据库?

向表中添加时出现Null异常

您正在添加null作为记录,当您在数据库中没有记录时,添加contact而不是currentAltContact。方法AddToSecondaryContact将有contact对象来添加新记录和currentAltContact当你想要更新记录。

if (currentAltContact == null)
{
    context.AddToSecondaryContact(currentAltContact);
}

if (currentAltContact == null)
{
    context.AddToSecondaryContact(contact);
}

实际上,我通过简单地在顶部声明currentAltContact变量并仅在存在空记录时实例化新对象来实现它

public void SaveAltContact(int prospectID, int contactPosition, SecondaryContact contact)
    {
        using (var context = new CoyleHomeBuyerEntities())
        {
                SecondaryContact currentAltContact;
            currentAltContact = (from sec in context.SecondaryContact
                                 where sec.ContactPosition == contactPosition
                                 where sec.ProspectID == prospectID
                                 select sec).FirstOrDefault();
            if (currentAltContact == null)
            {
                currentAltContact = new Model.SecondaryContact();
                context.AddToSecondaryContact(currentAltContact);
            }
            currentAltContact.FirstName = contact.FirstName;
            currentAltContact.LastName = contact.LastName;
            currentAltContact.EmailAddress = contact.EmailAddress;
            currentAltContact.PhoneNumber = contact.PhoneNumber;
            currentAltContact.SecondaryContactTypeID = contact.SecondaryContactTypeID;
            currentAltContact.ProspectID = prospectID;
            currentAltContact.ContactPosition = contactPosition;
            context.SaveChanges();
        }
    }