简体   繁体   中英

How to Update multiple tables with foreign keys using LINQ expression ASP.NET MVC Entity Framework

I am new to programming, and I have been trying to update multiple tables using LINQ expressions,

using (ADBEntities _ADBEntities = new ADBEntities())
{
    try
    {
        tblPerson _Person = new tblPerson();
        _Person.Id = PersonDetails.Id;
        _Person.Firstname = PersonDetails.Firstname;
        _Person.LastName = PersonDetails.LastName;
        _Person.DOB = PersonDetails.DOB;
        _Person.SSN = PersonDetails.SSN;

        var updatePersonDetails = _IPersonRepository.Update(_Person);

        if (updatePersonDetails != null)
        {   
            tblAddress _Address = new tblAddress();
            _Address.PersonId = updatePersonDetails.Id;
            var updateAddressDetails = _ADBEntities.tblAddresses.Find(_Address.PersonId);
            _Address.Id = updateAddressDetails.Id;
            _Address.Address = PersonDetails.Address;

            _ADBEntities.Entry(updateAddressDetails).CurrentValues.SetValues(_Address);
            _ADBEntities.SaveChanges(); 

            if (updateAddressDetails != null)
            {
                tblEmail _Email = new tblEmail();
                _Email.PersonId = updateAddressDetails.PersonId;
                var updateEmailDetails = _ADBEntities.tblEmails.Find(_Email.PersonId);
                _Email.Id = updateEmailDetails.Id;
                _Email.Email = PersonDetails.Email;

                _ADBEntities.Entry(updateEmailDetails).CurrentValues.SetValues(_Email);
                _ADBEntities.SaveChanges();

                if (updateEmailDetails != null)
                {
                    tblPhone _Phone = new tblPhone();
                    _Phone.PersonId = updateEmailDetails.Id;
                    var updatePhoneDetails = _ADBEntities.tblPhones.Find(_Phone.PersonId);
                    _Phone.Id = updatePhoneDetails.Id;
                    _Phone.PhoneNo = PersonDetails.PhoneNo;

                    _ADBEntities.Entry(updatePhoneDetails).CurrentValues.SetValues(_Phone);
                    _ADBEntities.SaveChanges();
                }
            }
        }
    }
}

This worked fine at the beginning, but now it throws an "object null reference" exception.

Tables are

tblPerson
tblAddress
tblPhone
tblEmail

You do not Test your "Find", if it is successfull. Probably this is null. The pattern you use to update an Item is kind of weired, I give you an example of your innermost update.

if (updateEmailDetails != null)
{
    tblPhone _Phone  = _ADBEntities.tblPhones.Find(updateEmailDetails.Id);
    if (_Phone != null)
       _Phone.PhoneNo = PersonDetails.PhoneNo;
}

That's all The SaveChanges you can call at the end, once for all updates

 _ADBEntities.SaveChanges(); 

What you have done with creating a new entry is sometimes done, to update an entry, without fetching it first from the Database. But you do Fetch it (with Find). In this case you can go the simple default path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM