按代码映射 - 双向关系

时间:2013-09-02 08:35:00

标签: c# nhibernate orm mapping-by-code

我有一个Parent-Children关系,目前只存在从每个子节点到父节点的引用(子表上有一个外键列)。我想将一个只读IEnumerable属性添加到父级,但我没有这样做。

public class ProjectData : Entity
{
    public virtual long Id {get;set;}
    private readonly IList<BranchData> _branches = new List<BranchData>();
    public virtual IEnumerable<BranchData> Branches
    {
        get { return _branches.AsReadOnly(); }
    }
}

public class BranchData : Entity
{ 
    puplic virtual long Id {get;set;}
    public virtual ProjectData ProjectData { get; set; }
}

 public class BranchDataMapping : ClassMapping<BranchData>
{
    public BranchDataMapping()
    {
       ManyToOne(x => x.ProjectData, mapper => { });
    }
}

  public class ProjectDataMapping : ClassMapping<ProjectData>
{
    public ProjectDataMapping()
    {
        Bag(data => data.Branches,
            mapper =>
                {
                    mapper.Access(Accessor.ReadOnly);
                    mapper.Inverse(false);                       
                }, relation => relation.OneToMany());

    }
}

表名和列名由约定创建。 当我创建并保存具有父属性集的新BranchDate时,它不会出现在父级的Branches集合中。

我缺少什么设置?

我已阅读Bi-directional NHibernate mapping by codehttp://notherdev.blogspot.cz/2012/02/nhibernates-mapping-by-code-summary.html,但仍未能正确设置。

编辑:验证此行为的代码

        [Test]
    public void CanSaveAndLoadBranch()
    {
        var session = InMemoryDatabase.GetSession();

        var project = new ProjectData {Name = "ratata"};
        var branch = new BranchData {ProjectData = project};

        using (var tx = session.BeginTransaction())
        {
            session.Save(project);
            session.Save(branch);

            tx.Commit();
        }
        var branches = project.Branches;
        //branches are empty

        var freshProject = session.Query<ProjectData>().Where(x => x.Id == project.Id).First();
        var freshBranches = freshProject.Branches;
        //branches are  empty here, too

        var fetched = session.Query<ProjectData>().Where(x => x.Id == project.Id).FetchMany(x => x.Branches).First();
        var fetchedBranches = fetched.Branches;
        //branches are  empty here, too


        var queriedOn = session.Query<ProjectData>().Where(x => x.Branches.Any()).ToList();
        //this does return the project, but Branches property is still empty


    }

1 个答案:

答案 0 :(得分:1)

在操作内存中的数据时,您负责管理关系的两个方面。您可以使用Add中的ProjectData方法轻松完成此操作:

public virtual void Add(BranchData branch)
{
    branch.ProjectData = this;
    Branches.Add(branch);
}

另一方面,NHibernate负责对物体进行适当的保湿和脱水。但是,您没有看到这一点,因为您的查询都发生在同一会话中。请考虑以下代码:

var project = new ProjectData();
session.Save(project);
session.Flush();
var freshProject = session.Get<ProjectData>(project.Id);
if (ReferenceEquals(project, freshProject))
    Console.WriteLine("They're the same instance.");

此代码将显示“它们是同一个实例”。该实例的关系设置与初始化时完全相同,无论是否正确。

如果忽略在反面设置关系project.Branches,NHibernate仍然可以保存它,但在该会话的持续时间内仍然会看起来不一致。如果您关闭会话并打开一个新会话(这可能会对您的内存中测试造成问题),或者只是致电session.Clear()session.Evict(project),那么您会在project.Branches时看到数据你再加载它。