Code-First EF4中的多对多关系 [英] Many-To-Many Relationship in Code-First EF4

查看:144
本文介绍了Code-First EF4中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在EF4 Code-First CTP3中表示多对多的关系?

How do you represent a many-to-many relationship in the EF4 Code-First CTP3?

例如,如果我有以下类:

For example if I have the following classes:

class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Profile> Profiles { get; set; }
}

class Profile
{
    public int Id { get; set; }
    public string Name { get; set; }
}

在数据库中有一个UserProfiles表具有用于用户的FK, FK for Profile。如何映射?

In the database there is a UserProfiles table that has the FK for User and FK for Profile. How can I map this?

编辑:我了解如何使用 ICollection< User> 财产在配置文件,但我真的不想有一个相反的导航属性,当它应该是用户有很多个人资料。

I understand how to to currently map with having a ICollection<User> property on the Profile, but I really don't want to have a an opposite navigation property when it should be "Users have many profiles".

推荐答案

编辑:CTP4昨天(2010年7月14日)发布了,现在支持this:

EDIT: CTP4 was released late yesterday (July 14 2010) and there is now support for this:

modelBuilder.Entity< Post>()。HasMany(p => p.Tags).WithMany();

我发现最终目前是不可能的。 Microsoft正在寻求添加此功能(只有一个导航属性)。

I found out finally that this currently isn't possible. Microsoft is looking to add this feature (only one navigation property).

有关详细信息,请参阅MSDN论坛上的此链接: http://social.msdn.microsoft.com/Forums/en/adonetefx/thread / 6920db2b-88c7-4bea-ac89-4809882cff8f

See this link on the MSDN forums for more information: http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/6920db2b-88c7-4bea-ac89-4809882cff8f

这篇关于Code-First EF4中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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