级联保存与流畅的NHibernate自动映射 [英] Cascade Saves with Fluent NHibernate AutoMapping

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

问题描述


$ b

如:

我保存人员和手臂也应该保存。目前我得到


对象引用未保存的瞬态实例 - 在冲洗前保存瞬态实例




  public class Person:DomainEntity 
{
public virtual Arm LeftArm {get;组; }
}

public class Arm:DomainEntity
{
public virtual int Size {get;组; }
}

我找到关于这个话题的文章,但似乎已经过时了。 >

解决方案

这适用于新的配置位。有关详情,请参阅 http://fluentnhibernate.wikia.com/wiki/Converting_to_new_style_conventions p>

  //挂断AutoPersistenceModel 
.ConventionDiscovery.AddFromAssemblyOf< CascadeAll>()


public class CascadeAll:IHasOneConvention,IHasManyConvention,IReferenceConvention
{
public bool Accept(IOneToOnePart target)
{
return true;
}

public void Apply(IOneToOnePart target)
{
target.Cascade.All();


public bool Accept(IOneToManyPart target)
{
return true;
}

public void Apply(IOneToManyPart target)
{
target.Cascade.All();
}

public bool接受(IManyToOnePart target)
{
return true;
}

public void Apply(IManyToOnePart target)
{
target.Cascade.All();
}
}


How do I "turn on" cascading saves using AutoMap Persistence Model with Fluent NHibernate?

As in:

I Save the Person and the Arm should also be saved. Currently I get

"object references an unsaved transient instance - save the transient instance before flushing"

public class Person : DomainEntity
{
  public virtual Arm LeftArm { get; set; }
}

public class Arm : DomainEntity
{
  public virtual int Size { get; set; }
}

I found an article on this topic, but it seems to be outdated.

解决方案

This works with the new configuration bits. For more information, see http://fluentnhibernate.wikia.com/wiki/Converting_to_new_style_conventions

//hanging off of AutoPersistenceModel    
.ConventionDiscovery.AddFromAssemblyOf<CascadeAll>()


public class CascadeAll : IHasOneConvention, IHasManyConvention, IReferenceConvention
{
    public bool Accept( IOneToOnePart target )
    {
        return true;
    }

    public void Apply( IOneToOnePart target )
    {
        target.Cascade.All();
    }

    public bool Accept( IOneToManyPart target )
    {
        return true;
    }

    public void Apply( IOneToManyPart target )
    {
        target.Cascade.All();
    }

    public bool Accept( IManyToOnePart target )
    {
        return true;
    }

    public void Apply( IManyToOnePart target )
    {
        target.Cascade.All();
    }
}

这篇关于级联保存与流畅的NHibernate自动映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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