流利的nHibernate:一对多的关系问题 [英] Fluent nHibernate: one-to-many relationship Issue

查看:120
本文介绍了流利的nHibernate:一对多的关系问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对多的关系问题。我有以下的域类:

$ $ p $ public class安装:实体<安装>
{
public virtual string Name {get;组; }
公共虚拟IList< Institution>机构{get;组; }
$ b $ public安装()
{
Institutions = new List< Institution>();


public class Institution:Entity
{
public virtual string Name {get;组; }
public virtual string Address {get;组; }
public virtual string City {get;组; }
公共虚拟安装安装{get;组; }



$ b我已经根据下面的 post 。我有以下定义的映射:

pre $ public class InstitutionMapping:ClassMap< Institution>
{
public InstitutionMapping()
{
WithTable(Institution);
Id(i => i.Id).GeneratedBy.Guid();
Map(i => i.Name).Not.Nullable()。WithLengthOf(50);
Map(i => i.Address).Not.Nullable()。WithLengthOf(50);
Map(i => i.City).Not.Nullable()。WithLengthOf(50);
引用(i => i.Installation).ColumnName(InstallationId)。Not.Nullable()。WithForeignKey();
}
}

public class InstallationMapping:ClassMap< Installation>
{
public InstallationMapping()
{
WithTable(Installation);
Id(i => i.Id).GeneratedBy.Guid();
Map(i => i.Name).Not.Nullable()。WithLengthOf(50);
HasMany< Institution>(i => i.Institutions).KeyColumnNames.Add(InstallationId)。Inverse()。Cascade.All();




$ b $ p
$ b

方式:

 安装安装= TestHelper.CreateAnonymousInstallation(); 
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
session.Save(安装);
session.Flush();
session.Clear();
安装returnedInstallation = session.Get<安装>(installation.Id);
Assert.AreEqual(2,returnedInstallation.Institutions.Count);

我得到一个断言异常,因为返回的机构数是0。并且机构被保存在数据库中,但是它们的InstallationId为空。有人告诉我我做错了什么?

解决方案

当你有一个持久集合 inverse = false,则父对象拥有该关系,并且对父集合的任何更改都将反映在数据库中。



一个带有 inverse =true的持久集合,那么子对象拥有这个关系,并且对子对父对象的引用的任何修改都会反映到数据库中。



因为您设置了 inverse =true,您需要将子对象的引用更改为父对象NHibernate拿起它。如果你希望NHibernate在父子集合中添加子元素或将子元素从父元素集合中移除时,必须设置 inverse =false


I have a problem with one-to-many relationships. I have the following domain classes:

public class Installation : Entity<Installation>
{        
    public virtual string Name { get; set; }
    public virtual IList<Institution> Institutions { get; set; }

    public Installation()
    {
        Institutions = new List<Institution>();
    }
}
public class Institution : Entity
{
    public virtual string Name { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual Installation Installation { get; set; }        
}

I have made the Entity base class according to the following post. I have the following mappings defined:

public class InstitutionMapping : ClassMap<Institution> 
{
    public InstitutionMapping()
    {
        WithTable("Institution");
        Id(i => i.Id).GeneratedBy.Guid();
        Map(i => i.Name).Not.Nullable().WithLengthOf(50);
        Map(i => i.Address).Not.Nullable().WithLengthOf(50);
        Map(i => i.City).Not.Nullable().WithLengthOf(50);
        References(i => i.Installation).ColumnName("InstallationId").Not.Nullable().WithForeignKey();
    }
}

public class InstallationMapping : ClassMap<Installation>
{
    public InstallationMapping()
    {
        WithTable("Installation");
        Id(i => i.Id).GeneratedBy.Guid();
        Map(i => i.Name).Not.Nullable().WithLengthOf(50);
        HasMany<Institution>(i => i.Institutions).KeyColumnNames.Add("InstallationId").Inverse().Cascade.All();
    }
}

I unit test adding institutions to an installation in the following way:

Installation installation = TestHelper.CreateAnonymousInstallation();
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
session.Save(installation);    
session.Flush();
session.Clear();
Installation returnedInstallation = session.Get<Installation>(installation.Id);
Assert.AreEqual(2, returnedInstallation.Institutions.Count);

I get an assertion exception because the returned number of institutions is 0. I have checked in the SQL Profiler and the institutions are saved in the database but their InstallationId is null. Could somebody tell me what I am doing wrong?

解决方案

When you have a persistent collection with inverse="false", then the parent object owns the relationship and any changes to the parent's collection will be reflected in the database.

When you have a persistent collection with inverse="true", then the child object owns the relationship and any changes to the child's reference to the parent will be reflected in the database.

Because you set inverse="true", you will need to change the child object's reference to the parent object in order for NHibernate to pick up on it. If you wish NHibernate to pick up on the changes to the relationship whenever you add children to or remove children from the parent's collection, you must set inverse="false" on the collection.

这篇关于流利的nHibernate:一对多的关系问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆