在没有对象属性的Fluent NHibernate中映射外键

Ann*_*e B 6 c# nhibernate nhibernate-mapping fluent-nhibernate

我的问题是,对于父对象和子对象是否有可能的Fluent NHibernate映射,它不需要Child对象具有Parent对象属性?我还没弄明白如何将引用映射回Parent.当我使用映射调用Create时,我得到一个异常,因为Child对象没有所需的外键(在数据存储中需要)回到Parent.

我有两个POCO课程:

public class Parent
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Child> Childs { get; set; }
}

public class Child
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual int ParentId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

还有一些映射:

public class ParentMap : ClassMap<Parent>
{
    public ParentMap()
    {
        this.Table("Parents");
        this.Id(x => x.Id);
        this.Map(x => x.Name);
        this.HasMany(x => x.Childs).KeyColumn("ChildId").Cascade.AllDeleteOrphan();
    }
}

public class ChildMap : ClassMap<Child>
{
    public ChildMap()
    {
        this.Table("Childs");
        this.Id(x => x.Id);
        this.Map(x => x.Name);
        // Needs some sort of mapping back to the Parent for "Child.ParentId"
    }
}
Run Code Online (Sandbox Code Playgroud)

并创建方法:

public Parent Create(Parent t)
{
    using (this.session.BeginTransaction())
    {
        this.session.Save(t);
        this.session.Transaction.Commit();
    }
    return t;
}
Run Code Online (Sandbox Code Playgroud)

我希望能够创建一个具有Child对象列表的Parent对象,但不能让Child对象返回其父对象(除了Parent ID).我想这样做是为了避免从Parent到Childs列表的循环引用回到Parent对象,因为这会导致JSON序列化问题.

Fel*_*ani 3

您可以毫无问题地映射这些实体,请尝试以下操作:

public class ParentMap : ClassMap<Parent>
{
    public ParentMap()
    {
        this.Table("Parents");
        this.Id(x => x.Id);
        this.Map(x => x.Name);
        this.HasMany(x => x.Childs).KeyColumn("ChildId").Cascade.AllDeleteOrphan();
    }
}

public class ChildMap : ClassMap<Child>
{
    public ChildMap()
    {
        this.Table("Childs");
        this.Id(x => x.Id);
        this.Map(x => x.Name);
        this.Map(x => x.ParentId);
        // if you have a reference of Parent object, you could map as a reference, for sample:
        this.References(x => x.Parent).Column("ParentId");
    }
}
Run Code Online (Sandbox Code Playgroud)

当您从 ISession 获取实体时,不要将其序列化为某种格式,因为这些可以是 nhibernate 的代理而不是实体对象。尝试创建DTO(数据传输对象)类并将这些实体转换为DTO对象,并将其序列化。您将避免循环引用。对于样品:

public class ParentDTO
{   
    public int Id { get; set; }
    public string Name { get; set; }
    public int ParentId { get; set; }

    /* here you just have the primitive types or other DTOs, 
       do not reference any Entity type*/
}
Run Code Online (Sandbox Code Playgroud)

当您需要读取值以共享序列化值时:

var dto = ISession.Query<Parent>()
                  .Select(x => 
                      new ParentDTO() { 
                           Id = x.Id, 
                           Name = x.Name, 
                           ParentId = x.ParentId)
                  .ToList();
Run Code Online (Sandbox Code Playgroud)

从数据访问层获取此结果并尝试序列化,例如:

var result = Serialize(dto);
Run Code Online (Sandbox Code Playgroud)