0

我有两节课

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();
        }
    }

我认为映射有问题。

4

1 回答 1

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();
于 2013-07-04T19:31:55.140 回答