Null因延迟加载而返回

时间:2015-02-23 22:16:02

标签: c# .net asp.net-web-api lazy-loading

我的测试环境设置如下:

class TestAppContext : ContextInterface
    {

        public DbSet<Person> Persons {get; set;}
        public DbSet<House> Houses { get; set; }

        public TestAppContext()
        {
            this.Persons = new TestPersonDbSet();
            this.Houses = new TestHousesDbSet();
        }

        public int SaveChanges(){
            return 0;
        }
        public void MarkAsModified(Object item) {

        }

        public void Dispose() { }

    }

模型看起来像这样:

class Person{
    [Key]
    public int id {get; set;}
    public string name {get; set;}
}

class House{
    [Key]
    public int id {get; set;}
    public string address {get; set;}
    public int personId {get; set;}
    [ForeignKey("personId")]
    public virtual Person person {get; set;}
}

现在,如果我运行此单元测试:

[TestMethod]
        public void GetServiceRequest_User()
        {
            TestAppContext context = new TestAppContext();
            House house = new House() {id=1, address="Middle of Nowhere", personId=3};
            Perosn person = new Person() {id=3, name="Some Dude"};
            context.Houses.Add(house);
            context.Persons.Add(person);

            HousesController controller = new HousesController(context);
            var resultRaw = controller.GetHouse(house.id);
            Assert.IsInstanceOfType(resultRaw, typeof(OkNegotiatedContentResult<House>));

            OkNegotiatedContentResult<House> result = resultRaw as OkNegotiatedContentResult<House>;
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Content.person);
            Assert.AreEqual(result.Content.person.id, result.Content.personId);
            Assert.AreEqual(result.Content.person.id, person.id);
        }

由于result.Content.person为null,它失败了。为什么是这样?据我所知,这个人应该懒得加载,因为ids匹配,但似乎并非如此。

按要求,GetHouse方法:

[Route("{id:int}", Name="DisplayHouse")]
        [ResponseType(typeof(House))]
        public IHttpActionResult GetHouse(int id)
        {
            House house = db.Houses.Find(id);
            if (house == null)
            {
                return NotFound();
            }

            return Ok(house);
        }

1 个答案:

答案 0 :(得分:1)

您可以尝试这样做,因为您将Id设置为Key我不认为您应该设置值,将人员添加到房子应该连接关系。由于您没有保存上下文延迟加载不适用。为此,您需要将更改保存到上下文中。

    [TestMethod]
    public void GetServiceRequest_User()
    {
        TestAppContext context = new TestAppContext();
        House house = new House() {address="Middle of Nowhere"};
        Perosn person = new Person() {name="Some Dude"};
        house.person = person;
        context.Houses.Add(house);


        HousesController controller = new HousesController(context);
        var resultRaw = controller.GetHouse(house.id);
        Assert.IsInstanceOfType(resultRaw, typeof(OkNegotiatedContentResult<House>));

        OkNegotiatedContentResult<House> result = resultRaw as OkNegotiatedContentResult<House>;
        Assert.IsNotNull(result);
        Assert.IsNotNull(result.Content.person);
        Assert.AreEqual(result.Content.person.id, result.Content.personId);
        Assert.AreEqual(result.Content.person.id, person.id);
    }