[英]Code First Entity Framework C#, Many to Many Mapping, Junction Table Created but list property not populated on object
Many Users can Send Many Message Requests to Many Other Users 许多用户可以向许多其他用户发送许多消息请求
USERS 用户
Id ID
Name 名称
Age 年龄
MESSAGE REQUESTS 留言要求
Id ID
Message 信息
FromUserID FromUserID
ToUserID ToUserID
ICollection<User> RequestUsers
I am trying to set up my class for this using code first. 我试图首先使用代码为此设置我的课程。 I have managed to get code first and EF to create the junction table but this is no good as the MessageRequest junction table is only used by the database.
我已经设法先获取代码并使用EF创建联结表,但这并不好,因为MessageRequest联结表仅由数据库使用。 I am looking for a way to add it to part of the context so I can work with it in code.
我正在寻找一种将其添加到部分上下文中的方法,以便可以在代码中使用它。
I want to be able to get a list of all users that have had a message request sent to them based on the Message Request ID. 我希望能够获得基于消息请求ID向其发送消息请求的所有用户的列表。
Even though code first created the junction table, when I debug the application and inspect RequestUsers property it is null and I want it to be populated automatically to work with in code. 即使代码首先创建了联结表,但当我调试应用程序并检查RequestUsers属性时,该属性为null,我希望将其自动填充以在代码中使用。
I have tried all kinds of mappings but can't get it to work. 我尝试了各种映射,但无法正常工作。
Can someone please explain how to layout the two classes for code first in ER and how I can get the RequestUsers Property to be populated automatically with the list of related users? 有人可以解释一下如何在ER中首先布局两个代码类,以及如何让RequestUsers属性自动填充相关用户列表吗?
Any time you're talking about EF classes, you should post the class definitions. 每当您谈论EF类时,都应该发布类定义。 It's nearly impossible to answer a question like this otherwise.
否则,几乎不可能回答这样的问题。 That said, I can take a guess that you neglected to add the
virtual
keyword to your collection, ie: 也就是说,我可以猜测您忽略了将
virtual
关键字添加到集合中,即:
public virtual ICollection<User> RequestUsers { get; set; }
Without virtual
EF will not load the relationship. 没有
virtual
EF将不会加载该关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.