FluentNHibernate,从另一个表获取 1 列
我们正在使用 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:连接表并从其他表获取单列,但我试图弄清楚如何使用 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));
}
}
使用该映射,我收到此错误:
名称空间“urn:nhibernate-mapping-2.2”中的元素“class”在名称空间“urn:nhibernate-mapping-2.2”中具有无效的子元素“join”。预期可能元素的列表:命名空间“urn:nhibernate-mapping-2.2”中的“joined-subclass、loader、sql-insert、sql-update、sql-delete、filter、resultset、query、sql-query”。
有什么想法吗?
We're using FluentNHibernate and we have run into a problem where our object model requires data from two tables like so:
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; }
}
Where there is a MyModel table that has Id, Name, and FooId as a foreign key into the Foo table. The Foo tables contains Id and FooName.
This problem is very similar to another post here: Nhibernate: join tables and get single column from other table but I am trying to figure out how to do it with FluentNHibernate.
I can make the Id, Name, and FooId very easily..but mapping FooName I am having trouble with. This is my class map:
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));
}
}
with that mapping I get this error:
The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'join' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'joined-subclass, loader, sql-insert, sql-update, sql-delete, filter, resultset, query, sql-query' in namespace 'urn:nhibernate-mapping-2.2'.
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你在这里误解了一些东西。
您需要创建两个类
以及它们的映射类,而不是在同一个类中包含 Id、Name、FooId 和 FooName:
这应该会有所帮助。
但请记住约定其他配置:)
I think you misunderstood something here.
Instead of having Id, Name, FooId and FooName in same class you need to create two classes
And your mapping classes for these:
this should help.
But remember Convention other Configuration :)