我用流利的如下映射兩類:NHibernate的會話刷新基類對象上調用導致無效強制轉換異常
一,應用類:
public ThirdPartyAppMap()
{
Schema("Data");
Id(x => x.ThirdPartyAppId).GeneratedBy.Identity();
Map(x => x.AppName);
Map(x => x.CreationTime);
Map(x => x.Enabled);
References(x => x.ThirdPartyCustomer, "ThirdPartyCustomerId").Class(typeof(ThirdPartyCustomer));
HasMany(x => x.ThirdPartyUsersIds).Table("Data.ThirdPartyUser").KeyColumn("ThirdPartyAppId").Element("UserId").AsBag();
}
這是我的核心領域的簡單應用程序
和子類的它:ApplicationExt映射:
public ThirdPartyAppProxExtMap()
{
Table("Data.ThirdPartyApp");
KeyColumn("ThirdPartyAppId");
this.HasManyIds(x => x.DeviceToAppRelIds, "Data.ThirdPartyDeviceToAppRel", "AppId", "ThirdPartyDeviceToAppRelId");
}
它映射到同一個表,但添加了hasmany屬性,它是一個id列表。 該類位於基於核心域的較高級別域中,核心域不需要知道應用程序類的實例,而較高域則可以。
映射工作正常,但我遇到異常與NH會話對象工作時:
當我打電話
Session.Refresh(BaseAppObject)
我收到以下異常:
Unable to cast object of type 'RedPill.Applicative.Module.CM.Domain.ThirdPartyApp' to type 'RedPill.Applicative.Module.PI.Domain.ThirdPartyAppProxExt'.
它拋出invalidcastexception,因爲它試圖將數據分配給BaseAppObject上不存在的擴展類型屬性,僅在ExtAppObject上。 看着分析器,sesion對象試圖獲取ExtApplicationObject而不是BaseApplicationObject。 所以基本上,我沒有Session.Get(),收到BaseAppObject它工作正常,但當我試圖刷新它使用Session.Refresh()它試圖獲取ExtApplicationObject,當NH試圖從選擇構建對象時它拋出無效的轉場異常。
對此有幫助嗎?
問候,
詹姆斯
不應該使用鑑別符而不是子類來映射它嗎?我認爲子類映射必須有你自己想要的任何擴展屬性的表。 – Fourth 2012-03-13 19:47:57
@Fourth http://www.philliphaydon.com/2011/08/fluent-nhibernate-table-inheritance-discriminators/,不,在我的情況下,我想創建一個不基於列中的值,但基於通用T型im從會話中請求不同的T有不同的映射,儘管它們都映射到同一個表。 – 2012-03-13 22:49:30
好吧, 我不知道這是否是最好的解決方案,但我所做的是更改我的數據庫模型,而不是一個表,兩個實體映射到它,其中一個實體派生另一個實體,是我分裂表分成兩個表,一個派生另一個,現在它是一個簡單的流利NHibernate的繼承 - 也許它更好的建模,或者有時你只需要適應你正在使用的工具... – 2012-03-13 23:08:06