我有点奇怪的错误让我有点困惑,所以让我描绘一下:
我有一个控制器,用房屋评论更新数据库。如果房子不存在,它会创建一个,然后更新它。
所以这是房子不存在的代码:
if (checkHouse == null)
{
var department = db.Universitys.Single(d => d.universityName == "NotSet");//make dynamic
var hws = new House();
hws.address1 = reviewmodelview.Address1;
hws.address2 = reviewmodelview.Address2;
hws.postCode = reviewmodelview.postCode;
hws.noOfDisputes = 0;
department.Houses.Add(hws);
db.SaveChanges(); //created the house and saved the changes, now apply the review to it.
var theHouse = db.Houses.Single(d => d.address1 == reviewmodelview.Address1);//findhouse
var rvw = new Review(); //create review
rvw.condition = reviewmodelview.condition;
rvw.furniture = reviewmodelview.furniture;
rvw.overall = reviewmodelview.overall;
rvw.isApproved = false;
rvw.review = reviewmodelview.review;
rvw.user = User.Identity.Name;
rvw.reviewDate = DateTime.Now;
theHouse.Reviews.Add(rvw);
db.SaveChanges();
}
以下是房子确实存在的代码:
else
{
var theHouse = db.Houses.Single(d => d.address1 == reviewmodelview.Address1);//findhouse
var rvw = new Review();//create review
rvw.condition = reviewmodelview.condition;
rvw.furniture = reviewmodelview.furniture;
rvw.overall = reviewmodelview.overall;
rvw.isApproved = false;
rvw.review = reviewmodelview.review;
rvw.user = User.Identity.Name;
rvw.reviewDate = DateTime.Now;
theHouse.Reviews.Add(rvw);
db.SaveChanges();
}
现在的问题是,如果房子存在,则添加评论。如果房子不存在,房子将被添加到数据库,但代码中断:
theHouse.Reviews.Add(rvw);
如果我将鼠标悬停在评论上,则说它为空。 错误是“对象引用未设置为对象的实例。”
但是,如果我再试一次,但这次添加数据库中已存在的房屋地址并在
处添加断点theHouse.Reviews.Add(rvw);
它说:{System.Data.Objects.DataClasses.EntityCollection<Data.Manager.Review>}
我无法弄清楚为什么两个相同的代码片段的评论集合为空!
房屋模型是:
namespace Data.Manager
{
public class House
{
public virtual int HouseID { get; set; }
public virtual string postCode { get; set; }
public virtual string address1 { get; set; }
public virtual string address2 { get; set; }
public virtual int noOfDisputes { get; set; } //number of disputes added by tennants
public virtual int averageRating { get; set; }
public virtual ICollection<Review> Reviews { get; set; }
}
}
如果我能解释为什么会这样,我会永远爱你。
答案 0 :(得分:6)
如果您使用ICollection<Review> Reviews
创建新房,则必须先将其初始化,然后才能使用Reviews.Add
。它适用于已经存在的房屋的原因是,当您加载现有房屋时,集合将被初始化并填充现有值。
在致电theHouse.Reviews = new List<Review>();
之前,您必须按照theHouse.Reviews.Add
的规定行事。您也可以在House
的构造函数中提前初始化它,以确保在任何访问尝试之前对其进行初始化。
答案 1 :(得分:2)
尝试在House构造函数中初始化您的评论集合
namespace Data.Manager
{
public class House
{
public House()
{
Reviews = new List<Review>();
}
public virtual int HouseID { get; set; }
public virtual string postCode { get; set; }
public virtual string address1 { get; set; }
public virtual string address2 { get; set; }
public virtual int noOfDisputes { get; set; } //number of disputes added by tennants
public virtual int averageRating { get; set; }
public virtual ICollection<Review> Reviews { get; set; }
}
}
答案 2 :(得分:0)
如上一个答案所述,您需要在向其添加项目之前初始化Review对象。
只需保存一次更改而不是两次调用数据库,就可以使代码略微提高。
if (checkHouse == null)
{
var department = db.Universitys.Single(d => d.universityName == "NotSet");//make dynamic
var hws = new House();
hws.address1 = reviewmodelview.Address1;
hws.address2 = reviewmodelview.Address2;
hws.postCode = reviewmodelview.postCode;
hws.noOfDisputes = 0;
hws.Reviews = new List<Review>();
var rvw = new Review(); //create review
rvw.condition = reviewmodelview.condition;
rvw.furniture = reviewmodelview.furniture;
rvw.overall = reviewmodelview.overall;
rvw.isApproved = false;
rvw.review = reviewmodelview.review;
rvw.user = User.Identity.Name;
rvw.reviewDate = DateTime.Now;
hws.Reviews.Add(rvw);
department.Houses.Add(hws);
db.SaveChanges();
}