2011-08-19 89 views
4

TL; DR:我试图在多对多表上添加多列多列表Order列。 HBM效果很好。无法获得FluentNHibernate映射的工作。将HBM映射到FluentNHibernate的多对多映射时遇到困难

我有以下两类其中我试图用许多一对多的关系映射:

public class User 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
    private IDictionary<int, Profile> profilesMap; 

    public virtual IEnumerable<Profile> Profiles { get { return this.profilesMap.Select(kvp => kvp.Value); } } 

    public User() 
    { 
     this.profilesMap = new SortedDictionary<int, Profile>(); 
    } 

    public virtual void Add(Profile x) 
    { 
     profilesMap.Add(x); 
    } 
} 

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

profilesMap的关键是轮廓顺序。因此,HBM映射如下:

<class name="User" table="User"> 
    <id name="Id" column="Id" type="integer"> 
    <generator class="native" /> 
    </id> 
    <property name="Name" type="string" column="Name" /> 
    <map name="profilesMap" access="field.camelcase" table="User_Profile"> 
    <key column="User_Id" /> 
    <index column="`Order`" type="integer" /> 
    <many-to-many class="Profile" column="Profile_Id" /> 
    </map> 
</class> 
<class name="Profile" table="Profile"> 
    <id name="Id" column="Id" type="integer"> 
    <generator class="native" /> 
    </id> 
    <property name="Name" type="string" column="Name" /> 
</class> 

这完美的作品,并创建正确的许多一对多表:

create table User_Profile (
    User_Id INT not null, 
    Profile_Id INT not null, 
    "Order" INT not null, 
    primary key (User_Id, "Order"), 
    constraint FK6BDEDC07D1EDE651 foreign key (Profile_Id) references Profile, 
    constraint FK6BDEDC07650CB01 foreign key (User_Id) references User 
) 

不过,我并不特别喜欢使用因为HBM这不是真的重构友好。因此,我试图将其转换为FluentNHibernate。这是我在尝试的用户的许多一对多映射:

public class UserMap : ClassMap<User> 
{ 
    public UserMap() 
    { 
     this.Id(); 
     this.Map(o => o.Name); 

     var mapMember = Reveal.Member<User, IEnumerable<KeyValuePair<int, Profile>>>("profilesMap"); 

     this.HasManyToMany<KeyValuePair<int, Profile>>(mapMember) 
      .Access.CamelCaseField() 
      .AsMap<int>("`Order`") 
      .Table("User_Profile"); 
    } 
} 

我预计这个工作,但它试图建立会话工厂时炸毁:

从表中的关联User_Profile引用了一个未映射的类:System.Collections.Generic.KeyValuePair`2 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[ConsoleApplication9.Profile,ConsoleApplication9,Version = 1.0。 0.0,文化=中立,PublicKeyToken = null]]

我如何获得AsMap相当多的研究工作,所以我把它改成如下:

this.HasManyToMany<KeyValuePair<int, Profile>>(mapMember) 
    .Access.CamelCaseField().AsMap<int>("`Order`") 
    .Element("Profile_id", ep => ep.Type<int>()) // Added. 
    .Table("User_Profile"); 

然而,这种由包括外键产生不正确的表(或NOT NULL约束)从Profile_id

0123:

create table User_Profile (
    User_id INT not null, 
    Profile_id INT, 
    "Order" INT not null, 
    primary key (User_id, "Order"), 
    constraint FK6BDEDC07650CB01 foreign key (User_id) references "User") 

此外,它也试图将配置文件添加到用户时炸毁

所以 - 我一直拉着我的头发几个小时试图确定如何正确地映射这种关系与FNH,但我似乎无法得到它。简单的多对多关系看起来很容易,但是当试图添加索引列时,它似乎不起作用。如果有人愿意帮我解决这个问题,我将不胜感激。

回答

2

您是否看到过类似问题的答案?

How to map IDictionary<string, Entity> in Fluent NHibernate

一个问题,我看到的是在你的多对多使用的元素。这可能是为什么你没有为profile_id获取任何外键设置。另外,如果可能的话,我会尝试为profilesMap添加公共访问器。

要匹配你的情况,我会想办法让你的映射如下:

HasManyToMany<Profile>(ProfilesMap) //Assuming the addition of a public get accessor 
    .Access.CamelCaseField() 
    .ParentKeyColumn("User_Id") 
    .ChildKeyColumn("Profile_Id") 
    .Table("User_Profile") 
    .AsMap<int>("Id"); 

如果您不能添加访问你可以试试这个

HasManyToMany<Profile>(Reveal.Member<User, object>("profilesMap")) 
    .Table("User_Profile") 
    .ParentKeyColumn("User_id") 
    .ChildKeyColumn("Profile_id") 
    .AsMap<int>("`Order`"); 
+0

完美!将第二个参数'Reveal.Member'作为'object' *真的做了一个清除事情的技巧(以及父/子键列)。我欠你的不仅仅是一个投票!干杯! – TheCloudlessSky

+0

另外 - 我希望你不介意,但我编辑你的答案,直接映射到OP的解决方案。再次感谢! – TheCloudlessSky