2010-05-28 56 views
3

获得1列,并在那里我们的对象模型的两个表中,像这样需要数据,我们碰到一个问题:FluentNHibernate,从另一个表我们使用FluentNHibernate

public class MyModel 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
    public virtual int FooId { get; set; } 
    public virtual string FooName { get; set; } 
} 

哪里有,有一个为MyModel表Id,Name和FooId作为Foo表中的外键。 Foo表包含Id和FooName。

这个问题是非常相似的另一篇文章在这里:Nhibernate: join tables and get single column from other table但我想弄清楚如何用FluentNHibernate做到这一点。

我可以很容易地使Id,Name和FooId ..但映射FooName我遇到了麻烦。这是我的类图:

public class MyModelClassMap : ClassMap<MyModel> 
{ 
    public MyModelClassMap() 
    { 
     this.Id(a => a.Id).Column("AccountId").GeneratedBy.Identity(); 
     this.Map(a => a.Name); 
     this.Map(a => a.FooId); 

     // my attempt to map FooName but it doesn't work 
     this.Join("Foo", join => join.KeyColumn("FooId").Map(a => a.FooName)); 
    } 
} 

一个映射我得到这个错误:

元素命名空间“类的“瓮:NHibernate的映射-2.2”在命名空间无效的子元素“加入” '瓮:NHibernate的映射-2.2'。预期的可能元素列表:在命名空间'urn:nhibernate-mapping-2.2'中的'joined-subclass,loader,sql-insert,sql-update,sql-delete,filter,resultset,query,sql-query'。

有什么想法?

回答

0

我想你在这里误解了一些东西。

,而不必在同一类ID,姓名,FooId和FooName您需要创建两个类

public class MyModel 
{ 
    public virtual int Id { get; set; } 

    public virtual string Name { get; set; } 

    public virtual Foo Foo { get; set; } 
} 

public class Foo 
{ 
    public virtual int Id { get; set; } 

    public virtual string FooName { get; set; } 
} 

你的映射类这些:

public class MyModelMapping : ClassMap<MyModel> 
{ 
    public MyModelMapping() 
    { 
     this.Id(x => x.Id); 
     this.Map(x => x.Name); 
     this.References(x => x.Foo); 
    } 
} 

public class FooMapping : ClassMap<Foo> 
{ 
    public FooMapping() 
    { 
     this.Id(x => x.Id); 
     this.Map(x => x.FooName); 
    } 
} 

这应该帮助。

但要记住公约其他配置:)

+0

是啊,这我怎么会传统上做,因为在本质上那是什么应该建模(特别是因为DB是这样设置) 但是我的要求(它来自以上)不想这样做...... 此外,它是为讨论起见的实际问题的简化。真正的问题是,如果我们按照您的建议来做,Foo类将拥有Foo2类型的属性,而Bar类将拥有Foo2类型的属性等等等等。 这意味着需要一堆不必要的当我需要的是一个属性时加入/子查询... – puffpio 2010-06-08 20:16:49