简体   繁体   English

代码优先实体框架C#,多对多映射,创建连接表,但未在对象上填充列表属性

[英]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.

相关问题 实体框架多对多,映射表和重复项。 代码优先 - Entity Framework Many to Many, mapping table & duplication. Code First 实体框架代码首先使用自定义用户和映射表进行多对多 - Entity Framework Code First Many to Many with Custom User and Mapping Table 在Entity Framework中创建多对多的联结表 - Creating many to many junction table in Entity Framework Entity Framework Junction(ish) 表映射代码优先 - Entity Framework Junction(ish) Table Mapping Code FIrst Code First Entity Framework中的Seed方法未填充多对多关系中的链接表 - The link table in Many-to-many relationship is not populated by the Seed method in Code First Entity Framework Entity Framework 5,Code First 多对多外键映射,具有额外的属性 - Entity Framework 5, Code First Many to Many foreign key mapping with extra property C#实体框架,如何从通过Junction表创建的对象中获取主键 - C# Entity framework, how to get the primary key from an object created from a Junction table 为实体框架中的多对多关系禁用隐式创建的联结表 - Disabling the Implicitly created junction table for many-to-many relationship in Entity Framework 首先在实体框架代码中映射多对多关系 - Mapping many to many relationship in entity framework code first 在多对多关系中映射查找/连接表实体 - Mapping lookup/junction table entity in many-to-many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM