1

我遇到了一个奇怪的错误,让我有点困惑,所以让我画图:

我有一个控制器,可以用房屋评论更新数据库。如果房子不存在,它会创建一个然后更新它。

所以这是如果房子不存在的代码:

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; }        
    }
}

如果我能解释为什么会这样,我会永远爱你。

4

3 回答 3

6

如果您要使用 来创建新房子ICollection<Review> Reviews,则必须先对其进行初始化,然后才能使用Reviews.Add. 它适用于已经存在的房屋的原因是,当您加载现有房屋时,集合将被初始化并填充现有值。

theHouse.Reviews = new List<Review>();在调用之前,您必须按照以下方式做一些事情theHouse.Reviews.Add。您还可以在 for 的构造函数中提前初始化House它,以确保在任何访问尝试之前对其进行初始化。

于 2013-02-22T15:47:34.773 回答
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; }        
    }
}
于 2013-02-22T15:57:30.187 回答
0

正如前面的答案所述,您需要在添加项目之前初始化评论对象。

您可以通过只保存一次更改而不是对数据库进行两次调用来使您的代码稍微好一些。

        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();

        }
于 2013-02-22T15:56:05.350 回答