流利的NHibernate一对一映射 [英] Fluent NHibernate One-to-One mapping
问题描述
请帮助定义关系的AMap和BMap类。 / b>
谢谢。
public class A
pre
{
public virtual int {get; set;}
公共虚拟字符串P1 {get; set;}
公共虚拟字符串P2 {get; set;}
公共虚拟字符串P3 { get; set;}
}
public class B
{
public virtual int Id {get; set;}
public virtual string P4 {get ; set;}
public virtual string P5 {get; set;}
public virtual string P6 {get; set;}
}
解决方案要获得
一对一
需要按照下面的代码将类型B
的属性添加到类A
中,反之亦然。这些引用在这两个类中都是必需的,因为NHibernate不支持单向的一对一。
public class A
{
public virtual int Id {get; set;}
公共虚拟字符串P1 {get; set;}
公共虚拟字符串P2 {get; set;}
public virtual字符串P3 {get; set;}
public virtual B child {get;组; }
}
public class B
{
public virtual int Id {get; set;}
public virtual string P4 {get; set;}
public virtual string P5 {get; set;}
public virtual string P6 {get; set;}
public virtual父类;
$ b然后在流畅的映射中,你需要添加下面的
$
$ b $ * b $ * $
HasOne(x => x.child)
.Cascade.All();
$ b $ public BMap()
{
/ *映射id和properties在这里* /
References(x => x.parent)
.Unique();
$ / code>请注意,
BMap
被标记为唯一
。这是用来创建一个独特的列约束,如果你使用NHibernate生成数据库模式。
要创建一个新的记录,然后你会写如下:
$ b $ pre $使用(var transaction = session.BeginTransaction())
{
var classA = new A();
classA.child = new B(){parent = classA};
session.Save(owner);
transaction.Commit();
$ / code>最后需要注意的是,NHibernate 3.4的当前版本不支持级联删除孤立的一对一。请参阅此处查看错误报告。这意味着如果你写了
session.Delete(classA);
之类的东西,那么相关的类B记录将不会自动删除。I am having a really hard time exploiting HasOne mapping with Fluent NHibernate. Basically, the class A can have a matching (only one or none) record in the class B.
Please help with the AMap and BMap classes that define the relationships.
Thank you.
public class A { public virtual int Id {get;set;} public virtual string P1 {get;set;} public virtual string P2 {get;set;} public virtual string P3 {get;set;} } public class B { public virtual int Id {get;set;} public virtual string P4 {get;set;} public virtual string P5 {get;set;} public virtual string P6 {get;set;} }
解决方案To get
one-to-one
mapping working you will need to add a property of typeB
to classA
and vice versa as per the code below. These references are required in both classes since NHibernate doesn't support unidirectional one-to-one.public class A { public virtual int Id {get;set;} public virtual string P1 {get;set;} public virtual string P2 {get;set;} public virtual string P3 {get;set;} public virtual B child { get; set; } } public class B { public virtual int Id {get;set;} public virtual string P4 {get;set;} public virtual string P5 {get;set;} public virtual string P6 {get;set;} public virtual A parent; }
Then in the fluent mappings you will need to add the following
public AMap() { /* mapping for id and properties here */ HasOne(x => x.child) .Cascade.All(); } public BMap() { /* mapping for id and properties here */ References(x => x.parent) .Unique(); }
Please note that the one-to-many mapping in
BMap
is marked asUnique
. This is used to create a unique column constraint if you use NHibernate to generate the DB schema.To create a new record you would then write something like:
using (var transaction = session.BeginTransaction()) { var classA = new A(); classA.child = new B() { parent = classA}; session.Save(owner); transaction.Commit(); }
Finally one caveat, the current release of NHibernate, 3.4, doesn't support cascade deletes of orphaned one-to-ones. See here for the bug report. This means if you write something like
session.Delete(classA);
then the associated class B record won't be automatically deleted.这篇关于流利的NHibernate一对一映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文