延迟加载返回 null

Null returned for 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 为空。为什么是这样?这个人,据我理解,应该是延迟加载,因为id匹配,但事实并非如此。

根据要求,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);
        }

你可以试试这个,因为你将 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);
    }