流畅的NHibernate一对一节省

时间:2012-12-05 13:12:33

标签: c# oracle nhibernate fluent-nhibernate fluent

我有3个班级:

public class WorkEntity
{
    public virtual int WorkId { get; set; }
    public virtual OrderExtensionEntity OrderExtension { get; set; }
}

public class OrderExtensionEntity
{
    public virtual int Id { get; set; }
    public virtual WorkEntity Work { get; set; }
    public virtual IList<IssueInformation> Issues { get; set; }
}

public class IssueInformationEntity
{
    public virtual int Id { get; set; }
    public virtual long WorkId { get; set; }
}

它们被映射:

public class WorkMap : ClassMap<WorkEntity>
{
    public WorkMap()
    {
        Id(x => x.WorkId).Column("ID_ZLECENIE").GeneratedBy.Sequence("SEQ_TAE_ZLECENIE");
        HasOne(x => x.OrderExtension).Constrained();
     }
}

public class OrderExtensionMap : ClassMap<OrderExtensionEntity>
{
    public OrderExtensionMap()
    {
        LazyLoad();

        Id(x => x.Id).Column("ID_ZLECENIE").GeneratedBy.Foreign("Work");
        HasMany(x => x.IssueInformation).KeyColumn("FK_ZLECENIE");
    }
}

public class IssueInformationMap : ClassMap<IssueInformationEntity>
{
    public IssueInformationMap()
    {
       Id(x => x.Id).Column("ID_ZAD_ROZ_ADRES").GeneratedBy.Sequence("SEQ_ZAD_ROZ_ADRES");

        Map(x => x.WorkId).Column("FK_ZLECENIE").Not.Nullable();
    }
}

当我在数据库(Oracle)上创建记录时,我可以从表中获取所有信息。 但我无法使用以下方法保存OrderExtensionEntity:

var orderExtension = new OrderExtension();
orderExtension._some_variables_like_strings_to_be_saved_

Session.SaveOrUpdate(orderExtension)

如果我尝试这样做,它会告诉我“未解决的属性工作”和其他一些错误:

orderExtension.Work = new WorkEntity() { WorkId = 12345 };

1 个答案:

答案 0 :(得分:0)

尝试此映射。这就是我的一对一关系的样子。

public class WorkMap : ClassMap<WorkEntity>
{
    public WorkMap()
    {
        Id(x => x.WorkId).Column("ID_ZLECENIE").GeneratedBy.Sequence("SEQ_TAE_ZLECENIE");
        HasOne(x => x.OrderExtension)
            .Cascade.All();
     }
}

public class OrderExtensionMap : ClassMap<OrderExtensionEntity>
{
    public OrderExtensionMap()
    {
        LazyLoad();

        Id(x => x.Id).Column("ID_ZLECENIE").GeneratedBy.Foreign("Work");
        HasOne(x => x.Work).Constrained();
        HasMany(x => x.IssueInformation).KeyColumn("FK_ZLECENIE");
    }
}