![](/img/trans.png)
[英]Entity framework: Null Reference Exception thrown when adding migration in package manager console VS 2012
[英]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);
}
}
}
}
如果你把LazyLoadingEnabled
和ProxyCreationEnabled
关你总是面对,因为使用的错误Department
后FirstorDefault
查询和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);
}
}
}
}
提示:不要忘记在AspNetUsers
和Groups
的构造函数中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.