我有以下数据库表,我为此示例进行了简化:
网页ID、URLIdentifier
WebpageMetaData Webpage_id 关键字
webpage_id 是表的主键和返回网页表的外键。
然后我使用 Fluent NHibernate 及其自动映射功能将这些映射到以下 POCO 类:
public class Webpage
{
public virtual int Id { get; set; }
public virtual string UrlIdentifier { get; set; }
public virtual WebpageMetaData WebpageMetaData { get; set; }
}
public class WebpageMetaData
{
public virtual int Id { get; set; }
public virtual string Keywords{ get; set; }
public virtual Webpage Webpage { get; set; }
}
我想将这些实体映射为一对一,因此我覆盖了自动映射,如下所示:
public class WebpageMap : IAutoMappingOverride<Webpage>
{
public void Override(AutoMapping<Webpage> mapping)
{
mapping.HasOne(w => w.WebpageMetaData)
.Not.Constrained()
.Fetch.Join()
.Cascade.All();
}
}
public class WebpageMetaDataMap : IAutoMappingOverride<WebpageMetaData>
{
public void Override(AutoMapping<WebpageMetaData> mapping)
{
mapping.Id(w => w.Id, "Webpage_id").GeneratedBy.Foreign("Webpage");
mapping.HasOne(w => w.Webpage)
.ForeignKey("Webpage_id")
.Constrained()
.Fetch.Join()
.Cascade.None();
}
}
我已经测试了这些映射和级联规则,它们适用于选择、插入和更新。但是,当我执行 WebpageMetaData 的删除时,我得到一个异常说
“已删除的对象将被级联重新保存(从关联中删除已删除的对象)[WebpageMetaData#524]”
停止异常的唯一方法是在保存之前执行以下操作:
webpageMetaData.Webpage.WebpageMetaData = null;
所以我必须从我的父母那里为自己设置一个空引用??!这似乎是错误的?我的映射错了吗?
谢谢。
更新 1: 请查看 Fluent 生成的 NH 映射,以防这对核心 NHibernator 有帮助:)
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="EveryPage.Core.Domain.Webpage, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Webpage`">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
<column name="Id" />
<generator class="identity" />
</id>
<one-to-one cascade="all" class="EveryPage.Core.Domain.WebpageMetaData, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" constrained="false" fetch="join" name="WebpageMetaData" />
<property name="UrlIdentifier" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="UrlIdentifier" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="EveryPage.Core.Domain.WebpageMetaData, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`WebpageMetaData`">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
<column name="Webpage_id" />
<generator class="foreign">
<param name="property">Webpage</param>
</generator>
</id>
<one-to-one cascade="none" class="EveryPage.Core.Domain.Webpage, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" constrained="true" fetch="join" foreign-key="Webpage_id" name="Webpage" />
<property name="Keywords" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Keywords" />
</property>
</class>
</hibernate-mapping>