我如何在FluentNHibernate中映射类?

时间:2013-07-03 09:43:01

标签: c# nhibernate orm fluent-nhibernate

我有两个班级

public class ProgramObject
{
   public virtual long Id { get; set; }
   public virtual string Name { get; set; }
   public virtual Commisioning planCommisioning { get; set; }
   public virtual Commisioning factCommisioning { get; set; }
}
public class Commisioning
{
    public virtual long Id { get; set; }
    public virtual decimal? FirstQuarter { get; set; }
    public virtual ProgramObject planCommisioningRef { get; set; }
    public virtual ProgramObject factCommisioningRef { get; set; }
}

和地图类

public class ProgramObjectMap : ClassMap<ProgramObject>
{
    public ProgramObjectMap()
    {
        Table("Ipo_ProgramObject");
        Id(x => x.Id).GeneratedBy.Increment();
        Map(x => x.SuplyOrgName).Column("SuplyOrgName");
        References(x => x.factPowerCommisioning).Column("factCommisioningID");
        References(x => x.planPowerCommisioning).Column("planCommisioningID");
    }
}
public class CommisioningMap : ClassMap<Commisioning>
    {
        public CommisioningMap()
        {
            Table("Ipo_Commisioning");
            Id(x => x.Id).GeneratedBy.Increment();
            Map(x => x.FirstQuarter).Column("FirstQuarter");
            HasOne(x => x.factCommisioningRef)
                .Cascade.All();
            HasOne(x => x.planCommisioningRef)
                .Cascade.All();
        }
    }

当我尝试提交时,我有一个异常“对象引用未保存的瞬态实例”。我在映射方面做错了什么?

要保存对象,请使用以下代码:

private static void Save(SessionManager sManager, ProgramObject ProgramObject, IpoConversions conversions)
    {
        sManager.OpenSession();
            var currSession = sManager.CurrentSession;
        using (ITransaction tx = currSession.BeginTransaction())
        {
            currSession.Save(ProgramObject);
            tx.Commit();
        }
    }

我认为映射存在问题。

1 个答案:

答案 0 :(得分:0)

您需要先保存通知或设置级联

currSession.Save(ProgramObject.factPowerCommisioning);
currSession.Save(ProgramObject.planPowerCommisioning);

// or

References(x => x.factPowerCommisioning).Column("factCommisioningID").Cascade.All();
References(x => x.planPowerCommisioning).Column("planCommisioningID").Cascade.All();