请帮助我将这个简单流畅的 nhibernate 映射变为现实
好吧,这一切看起来都很简单,但我找不到问题的答案。
假设我有 2 个表 TableA 和 TableB,它们可以保存对第三个表的单个引用:
TableA TableB TableC
ID ID ID
TableC_ID TableC_ID RandomDataC
RandomDataA RandomDataB
每个表都由以下类表示:
public Class ClassA{
public virtual int Id{get;set;}
public virtual ClassC ObjAC{get;set;}
...
}
public Class ClassB{
public virtual int Id{get;set;}
public virtual ClassC ObjBC{get;set;}
...
}
public Class ClassC{
public virtual int Id{get;set;}
...
}
如您所见,ClassA 和 ClassB 保存一个名为 ClassC 的单个属性。
我已经尝试了以下映射(除其他外......),
public Class ClassAMap : ClassMap<ClassA>{
public ClassAMap(){
Id(x => x.Id);
HasOne(x => x.ObjAC).Cascade.All();
...
}
}
public Class ClassBMap : ClassMap<ClassB>{
public ClassBMap(){
Id(x => x.Id);
HasOne(x => x.ObjBC).Cascade.All();
...
}
}
public Class ClassCMap : ClassMap<ClassC>{
public ClassCMap(){
Id(x => x.Id);
}
}
但映射不正确。我尝试使用 ClassCMap 来创建 ReferencesAny 但没有成功。
请帮忙...
Well it all seems so simple and yet I can't find the answer to my problem.
Let's say I have 2 tables TableA and TableB that can hold a single reference to a 3rd table:
TableA TableB TableC
ID ID ID
TableC_ID TableC_ID RandomDataC
RandomDataA RandomDataB
Each are represented by the following classes:
public Class ClassA{
public virtual int Id{get;set;}
public virtual ClassC ObjAC{get;set;}
...
}
public Class ClassB{
public virtual int Id{get;set;}
public virtual ClassC ObjBC{get;set;}
...
}
public Class ClassC{
public virtual int Id{get;set;}
...
}
As you can see ClassA and ClassB hold a single property called ClassC.
I've tried the following mapping (amongst others...)
public Class ClassAMap : ClassMap<ClassA>{
public ClassAMap(){
Id(x => x.Id);
HasOne(x => x.ObjAC).Cascade.All();
...
}
}
public Class ClassBMap : ClassMap<ClassB>{
public ClassBMap(){
Id(x => x.Id);
HasOne(x => x.ObjBC).Cascade.All();
...
}
}
public Class ClassCMap : ClassMap<ClassC>{
public ClassCMap(){
Id(x => x.Id);
}
}
but the mapping is incorrect. I've tried ClassCMap to have a ReferencesAny but without success.
Please help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
您可能还想确保推断出正确的表名。问题在于 HasOne 根据您要映射的表的主键映射到实体。
Try this:
You may also want to make sure the correct table names are being inferred. The issue is that HasOne maps to an entity based on the primary key of the table you're mapping from.
当使用 HasOne 时,这意味着您的表与其他表共享相同的主键。就你而言,这不是真的。 A、B、C 类都有自己的 id。查看以下文章,更详细地解释了这一点:
http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/
When using a HasOne this implies that your table shares the same primary key with the other table. In your case this is not true. Classes A, B, and C each have their own ids. Check out the following article that explains this in more detail:
http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/
您可能需要使用
References
而不是HasOne
。 HasOne 有一个独特的用例。将
HasOne
替换为References
。我也不认为 .Cascade.All() 是必要的。您可以在此处查看 HasOne 的用法:http://wiki. Fluentnhibernate.org/Fluent_mapping。
You 're probably needing to use
References
instead ofHasOne
. HasOne has a unique use case.Replace
HasOne
withReferences
. I don't believe .Cascade.All() is necessary either.You can check out the usage for HasOne here: http://wiki.fluentnhibernate.org/Fluent_mapping.