使用包括-EF时,导航属性为null [英] Navigation property is null when using Include - EF

查看:270
本文介绍了使用包括-EF时,导航属性为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打电话

Persons.Include(e => e.City)

Persons.Include(e => e.City)

在我的Web API的"Get()"方法中.

in "Get()" method in my web API.

城市"模型中的导航属性"ICollection人"返回null. 因此,当我使用"PostMan"获取值时,它将引发错误

The navigation property "ICollection persons" in "City" model return null. So when I use "PostMan" to get values it throw error

无法得到任何回应 连接到 http://localhost:13236/api/values 时出错.

Could not get any response There was an error connecting to http://localhost:13236/api/values.

我在GitHub上链接了一个测试项目: https://github.com/MajdAlbaho/SampleWebAPI

I linked a test project on GitHub : https://github.com/MajdAlbaho/SampleWebAPI

推荐答案

要启用延迟加载,必须将ICollection属性标记为virtual. 所以在您的示例中,而不是:

To enable lazy loading ICollection property must be marked as virtual. so in your example, instead of:

public ICollection<Person> Persons { get; set; }

这样定义它:

public virtual ICollection<Person> Persons { get; set; }

本文可能会有所帮助:实体框架加载相关实体

This article may be helpful: Entity Framework Loading Related Entities

这篇关于使用包括-EF时,导航属性为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆