簡體   English   中英

添加到橋表時,Entity Framework Null參考異常

[英]Entity Framework Null reference exception when adding to bridge tables

我正在將Entity Framework與MVC5應用程序一起使用,當前正在嘗試保存一些觸及數據庫中多個表的表單數據。 當我向表中添加數據時,它似乎工作正常,但是一旦我打了橋表,我就得到了一個null ref異常,對我而言,這是沒有意義的。

我是編程新手,所以將不勝感激。

public void RegisterNewUser(IDCRegisterViewModel model)
    {
        //
        string fullAddress = model.AddressLine1 + "\n" + model.AddressLine2 + (string.IsNullOrEmpty(model.AddressLine2) ? "" : "\n" ) + model.City + ", " + model.State + " " + model.Zip + "\n" + model.Country; 
        using (var AuthContext = new InfoKeeperEntities1())
        {
            AuthContext.Locations.Add(new Location {
                Line1 = model.AddressLine1,
                Line2 = model.AddressLine2,
                City = model.City,
                State = model.State,
                Zip = model.Zip,
                Country = model.Country,
                UserID = model.UserID,
                FullAddr = fullAddress
            });

            AuthContext.ProfileDatas.Add(new ProfileData
            {
                UserID = model.UserID,
                UACC = model.UACC,
                isRecCenter = model.IsRecCenter,
                isCustAdmin = model.IsCustAdmin
            });

            //Add to bridge tables for user/dept and group/dept
            List<Department> deptList = new List<Department>();
            foreach (var ID in model.GroupIDs)
            {
                deptList.Add(AuthContext.Departments.FirstOrDefault(x => x.ID == ID));
            }

            foreach (var department in deptList)
            {
                //NULL REF EXCEPTION HERE
                AuthContext.AspNetUsers.FirstOrDefault(x => x.Id == model.UserID).Departments.Add(department);

                foreach (var groupID in model.GroupIDs)
                {
                    AuthContext.Groups.FirstOrDefault(x => x.ID == groupID).Departments.Add(department);
                }
            }
        }
    }

如果你把LazyLoadingEnabledProxyCreationEnabled關你總是面對,因為使用的錯誤DepartmentFirstorDefault查詢和EntityFramework不包括它AppUsers,你必須與添加的部門向集團同樣的問題。 因此,您必須首先將這兩個部門都包括在內。

using System.Data.Entity;放置using System.Data.Entity; 在第一個代碼中 將代碼語句更改為此:

public void RegisterNewUser(IDCRegisterViewModel model)
{
    string fullAddress = model.AddressLine1 + "\n" + model.AddressLine2 + (string.IsNullOrEmpty(model.AddressLine2) ? "" : "\n" ) + model.City + ", " + model.State + " " + model.Zip + "\n" + model.Country; 
    using (var AuthContext = new InfoKeeperEntities1())
    {
        AuthContext.Locations.Add(new Location {
            Line1 = model.AddressLine1,
            Line2 = model.AddressLine2,
            City = model.City,
            State = model.State,
            Zip = model.Zip,
            Country = model.Country,
            UserID = model.UserID,
            FullAddr = fullAddress
        });

        AuthContext.ProfileDatas.Add(new ProfileData
        {
            UserID = model.UserID,
            UACC = model.UACC,
            isRecCenter = model.IsRecCenter,
            isCustAdmin = model.IsCustAdmin
        });

        //Add to bridge tables for user/dept and group/dept
        List<Department> deptList = new List<Department>();
        foreach (var ID in model.GroupIDs)
        {
            deptList.Add(AuthContext.Departments.FirstOrDefault(x => x.ID == ID));
        }
        var user = AuthContext.AspNetUsers.Include("Departments").FirstOrDefault(x => x.Id == model.UserID);         
        foreach (var department in deptList)
        {
            user.Departments.Add(department);

            foreach (var groupID in model.GroupIDs)
            {
                var group = AuthContext.Groups.Include("Departments").FirstOrDefault(x => x.ID == groupID);
                group.Departments.Add(department);
            }
        }
    }
}

提示:不要忘記在AspNetUsersGroups的構造函數中AspNetUsers List<Depatment> AspNetUsers List<Depatment>的新實例:

public class ApplicationUser
{
    Public ApplicationUser()
    {
        this.Departments = new List<Department>();
    }
}
public class Group
{
    Public Group()
    {
        this.Departments = new List<Department>();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM