我有一个一对多的关系的问题。 我有以下域类:
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; }
}
我已经按照下面的步骤进行实体基类岗位 。 我有以下映射定义:
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();
}
}
我的单元测试增加机构通过以下方式安装:
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);
我得到一个断言异常,因为机构的回数为0。我在SQL事件探查器检查和机构都保存在数据库中,但他们的InstallationId为空。 有人能告诉我什么,我做错了什么?