实体框架添加到桥接表时的空引用异常

时间:2017-06-19 17:26:07

标签: c# asp.net-mvc entity-framework

我正在使用带有MVC5应用程序的Entity Framework,目前我正在尝试保存一些触及数据库中多个表的表单数据。当我向表中添加数据时,它似乎工作正常但是一旦我点击桥表,我得到一个空引用异常,对我来说,没有意义。

我是编程新手,所以非常感谢任何帮助。

private void initButtons() {

    int locationX = 0, locationY = 525;

    for (int y = 0; y < 8; y++) {
        for (int x = 0; x < 8; x++) {
            boardArray[x][y] = new ChessButton();
            boardArray[x][y].setSize(75, 75);
            boardArray[x][y].setLocation(locationX, locationY);
            boardArray[x][y].setXAndY(x, y);

            if ((x % 2 == 0 && y % 2 == 1) || (x % 2 == 1 && y % 2 == 0)) {
                boardArray[x][y].setColour("white");
                boardArray[x][y].setIcon(new ImageIcon("Assets/white_null_null.png"));

            } else {

                boardArray[x][y].setColour("black");
                boardArray[x][y].setIcon(new ImageIcon("Assets/black_null_null.png"));

            }
//this adds the images in an alternating pattern

            chessFrame.add(boardArray[x][y]);
            locationX = locationX + 75;

        }
        locationX = 0;

        locationY = locationY - 75;

    }

}

void initPieces() {
    for (int y = 0; y < 8; y++) {
        for (int x = 0; x < 8; x++) {

            if ((x % 2 == 0 && y % 2 == 1) || (x % 2 == 1 && y % 2 == 0)) {
                boardArray[x][y].setFont(new Font("Arial Unicode MS", Font.PLAIN, 40));
                boardArray[x][y].setText("\u2654");//sets a particular chess piece as text, just testing it now.

            } else {

                boardArray[x][y].setFont(new Font("Arial Unicode MS", Font.PLAIN, 40));
                boardArray[x][y].setText("\u2654");//sets a particular chess piece as text, just testing it now.
//this is suposed to overlay the image over the text, but it is not.
            }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果您关闭LazyLoadingEnabledProxyCreationEnabled,则由于在Department查询后FirstorDefault使用EntityFramework并且using System.Data.Entity;不包括在内,因此您始终面临错误对于AppUsers,将部门添加到组中会遇到同样的问题。因此,您必须首先包括该部门。

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); } } } } 放在第一个代码中。 将代码语句更改为:

List<Depatment>

提示:不要忘记在AspNetUsersGroups的构造函数中创建public class ApplicationUser { Public ApplicationUser() { this.Departments = new List<Department>(); } } public class Group { Public Group() { this.Departments = new List<Department>(); } } 的新实例:

name_element = driver.find_element_by_xpath('//div[@class="div_input_place"]/input[@id="txt_name"]')
name_attribute = name_element.get_attribute("value")
print(name_attribute)