添加到表时出现Null异常

时间:2021-05-14 02:09:17

I am gathering data from a web-form with the code below:

我正在使用以下代码从网络表单收集数据:

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;

Sending the code to the repository to be saved with the code below (which it grabs the prospect.Id successfully):

将代码发送到存储库以使用下面的代码保存(它抓住了prospect.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);
    }
}

The repository trys to save the data:

存储库试图保存数据:

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();
    }
}

It always fails after this line with a Null exception:

在具有Null异常的此行之后总是失败:

context.AddToSecondaryContact(currentAltContact);

I'm baffled, because I expect the query above to return a NULL value at this point. SecondaryContactID is set to be the primary key and to auto increment.

我很困惑,因为我希望上面的查询在这一点上返回一个NULL值。 SecondaryContactID设置为主键和自动递增。

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

Can someone point me in the right direction as to why this is failing when trying to add a new row to the database?

在尝试向数据库添加新行时,有人能指出正确的方向吗?

2 个解决方案

#1


2  

You are adding null as record, Add contact instead of currentAltContact when you do not have record in database. The method AddToSecondaryContact will have contact object to add new record and currentAltContact when you want to update the record.

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

Change

更改

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

To

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

#2


0  

Actually I got it to work by simply declaring the currentAltContact variable at the top and instantiating a new object only if there was a null record

实际上我通过简单地在顶部声明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();
        }
    }

#1


2  

You are adding null as record, Add contact instead of currentAltContact when you do not have record in database. The method AddToSecondaryContact will have contact object to add new record and currentAltContact when you want to update the record.

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

Change

更改

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

To

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

#2


0  

Actually I got it to work by simply declaring the currentAltContact variable at the top and instantiating a new object only if there was a null record

实际上我通过简单地在顶部声明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();
        }
    }