流利的NHibernate一对多关系设置外键为null [英] Fluent NHibernate one-to-many relationship setting foreign key to null
问题描述
公共类申请人
{
公共申请人()
{
Tags =新列表< Tag>();
}
public virtual int Id {get;组; }
//为了示例而删除了其他字段
公共虚拟IList< Tag>标签{get;保护组}
public virtual void AddTag(Tag tag)
{
tag.Applicant = this;
Tags.Add(tag);
$ b $ public class Tag
{
public virtual int Id {get;保护组}
公共虚拟字符串TagName {get;组; }
公共虚拟申请人申请人{get;组; }
}
我的流利映射如下:
public class ApplicantMap:ClassMap< Applicant>
{
public ApplicantMap()
{
Id(x => x.Id);
HasMany(x => x.Tags).Cascade.All();
$ b $ public class TagMap:ClassMap< Tag>
{
public TagMap()
{
Id(x => x.Id);
Map(x => x.TagName);
引用(x => x.Applicant).Not.Nullable();
$ / code $ / pre
每当我尝试更新申请人(插入一个新的工作正常),它失败了,我看到在日志中的以下SQL异常:
11 :50:52.695 [6] DEBUG NHibernate.SQL - UPDATE [Tag] SET Applicant_id = null WHERE Applicant_id = @ p0; @ p0 = 37 [Type:Int32(0)]
11:50:52.699 [6]错误NHibernate.AdoNet.AbstractBatcher-无法执行命令:UPDATE [Tag] SET Applicant_id = null WHERE Applicant_id = @ p0 System.Data.SqlClient.SqlException(0x80131904):无法将NULL值插入'Applicant_id'列'RecruitmentApp .dbo.Tag';列不允许空值。更新失败。为什么NHibernate试图更新标签表并将Applicant_id设置为null?
< 解决方案 设置 Applicant.Tags
到 Inverse
将指示NHibernate在申请人$ c $>之后保存标签
public class ApplicantMap:ClassMap< Applicant>
{
public ApplicantMap()
{
Id(x => x.Id);
HasMany(x => x.Tags).Cascade.All()。Inverse();
$ b更多详情:
逆
(而不是 .Not.Inverse()
)意味着关系(在这种情况下,每个 Tag
)负责维护关系。因此,NHibernate知道必须首先保存 Applicant
,这样 Tag
对于它的<$ c有一个有效的外键$ c $> b
$ b
经验法则:包含外键的实体通常是所有者,所以另一个表应该有反向
I have a simple Fluent NHibernate model with two related classes:
public class Applicant
{
public Applicant()
{
Tags = new List<Tag>();
}
public virtual int Id { get; set; }
//other fields removed for sake of example
public virtual IList<Tag> Tags { get; protected set; }
public virtual void AddTag(Tag tag)
{
tag.Applicant = this;
Tags.Add(tag);
}
}
public class Tag
{
public virtual int Id { get; protected set; }
public virtual string TagName { get; set; }
public virtual Applicant Applicant { get; set; }
}
My fluent mapping is the following:
public class ApplicantMap : ClassMap<Applicant>
{
public ApplicantMap()
{
Id(x => x.Id);
HasMany(x => x.Tags).Cascade.All();
}
}
public class TagMap : ClassMap<Tag>
{
public TagMap()
{
Id(x => x.Id);
Map(x => x.TagName);
References(x => x.Applicant).Not.Nullable();
}
}
Whenever I try to update an applicant (inserting a new one works fine), it fails and I see the following SQL exception in the logs:
11:50:52.695 [6] DEBUG NHibernate.SQL - UPDATE [Tag] SET Applicant_id = null WHERE Applicant_id = @p0;@p0 = 37 [Type: Int32 (0)]
11:50:52.699 [6] ERROR NHibernate.AdoNet.AbstractBatcher - Could not execute command: UPDATE [Tag] SET Applicant_id = null WHERE Applicant_id = @p0 System.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'Applicant_id', table 'RecruitmentApp.dbo.Tag'; column does not allow nulls. UPDATE fails.
Why is NHibernate trying to update the tag table and set Applicant_id to null? I'm at a loss on this one.
解决方案 Set Applicant.Tags
to Inverse
will instruct NHibernate to save Tags
after the Applicant
.
public class ApplicantMap : ClassMap<Applicant>
{
public ApplicantMap()
{
Id(x => x.Id);
HasMany(x => x.Tags).Cascade.All().Inverse();
}
}
More detail:
Inverse
(as opposed to .Not.Inverse()
) means the other side of the relationship (in this case, each Tag
) is responsible for maintaining the relationship. Therefore, NHibernate knows that the Applicant
must be saved first so that Tag
has a valid foreign key for its Applicant
.
Rule of thumb: The entity containing the foreign key is usually the owner, so the other table should have Inverse
这篇关于流利的NHibernate一对多关系设置外键为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文