流利的NHibernate一对一映射 [英] Fluent NHibernate One-to-One mapping

查看:128
本文介绍了流利的NHibernate一对一映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用Fluent NHibernate的HasOne映射。基本上,类A可以在类B中有匹配的(只有一个或没有)记录。



请帮助定义关系的AMap和BMap类。 / b>

谢谢。

  public class A 
{
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;}
}
pre

解决方案

要获得一对一需要按照下面的代码将类型 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 type B to class A 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 as Unique. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆