实体框架中同一张表上的一对多关系
我有两个表,其中一个用户可以有多个联系人:
User(UserId, UserName)
Contact(UserId, ContactId)
假设我想通过 Userid 从用户表中的 UserNames 中获取所有 ContactNames。
注意Contact表在当前数据上下文中是看不到的,已经变成了多对多关系
如何查询?
如果需要插入或删除怎么办?
我尝试了“包含”方法,但它不起作用。您有什么建议吗?
非常感谢。
I have two tables, in which one user can have several contacts:
User(UserId, UserName)
Contact(UserId, ContactId)
Suppose I would like to get all the ContactNames from the UserNames in the User Table by the Userid.
Note that the Contact table cannot be seen in the current data context, it has been turned into a many to many relationship
How can I do the query?
What If I need to insert or delete?
I tried the "include" method but it does not work. Do you have any suggestions?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从数据库生成后,您的模型有 2 个子集合:Users 和 Users1。
您可以通过编辑器将它们重命名为代表其对 Contacts 和 ContactsFor 的含义。
如果您仍然想有 2 种类型而不是 Type+(Many-To-Many Ref),那么在编辑器中您可以删除引用、创建新实体联系人、设置所有映射、添加引用。完成所有这些后 - 您将拥有如下所示的模型:
要实现此目的:
Contact
Contact
的映射但这并不那么容易。
After generation from db your model has 2 sub-collections: Users and Users1.
You can rename them to represent their meaning to Contacts and ContactsFor via editor.
If you still want to have 2 types instead of Type+(Many-To-Many Ref), then in editor you can delete reference, create new entity Contact, setup all mapping, add references. After all this is done - you will have model look like this:
To Achieve this:
Contact
Contact
But it's not that easy.