Fluent NHibernate:外键不为空问题
我有以下域类:
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").Cascade.All();
}
}
当我运行以下代码时:
Installation installation = TestHelper.CreateAnonymousInstallation();
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
session.Save(installation);
出现以下错误:
NHibernate.PropertyValueException: not-null 属性引用 null 或 瞬态值。
如何克服这个问题呢?
提前致谢 卢卡斯·格拉兹
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").Cascade.All();
}
}
When I run the following code:
Installation installation = TestHelper.CreateAnonymousInstallation();
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
session.Save(installation);
I get the following error:
NHibernate.PropertyValueException:
not-null property references a null or
transient value.
How to overcome the problem?
Thanks in advance
Lukasz Glaz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信需要的是 inverse=true 。
或者当然可以在安装类中添加一个方法来显式处理此问题。
I believe it's inverse=true that is needed.
Or of course add a method in the installation class to handle this explicitly.
正如我所看到的,您的机构课程有关于安装课程的参考。并且这个引用被设置为不为空。当您创建 AnonymousInstitution 时,您是否为其设置了一些安装?
As I can see your Institution class has reference on Installation class. And this reference is set to not null. When you are creating AnonymousInstitution do you set some Installation for it?