我有一個對象,我試圖堅持一個遺留數據庫與NHibernate。我擁有映射到我的域對象中的表中的所有相關列,但表中有幾個字段必須填充爲遺留數據庫的結構。添加未映射的數據到NHibernate
遵循一些建議,我創建並註冊了一個NHibernate攔截器來爲我做這件事。
public class CustomLineItemInterceptor : EmptyInterceptor
{
public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
var lineItem = entity as SomeCustomLineItem;
if (lineItem == null)
return false;
List<string> propertyNameList = propertyNames.ToList();
List<IType> typeList = types.ToList();
List<object> stateList = state.ToList();
propertyNameList.Add("Source"); // the unmapped column in the database
typeList.Add(NHibernateUtil.String);
stateList.Add("My Application's Name"); // the value I need to persist
state = stateList.ToArray();
propertyNames = propertyNameList.ToArray();
types = typeList.ToArray();
return true;
}
}
我已經看了這個NHibernate add unmapped column in interceptor和Unmapped Columns in NHibernate?沒有找到答案。
我所看到的是OnSave方法會觸發,但是我沒有在數據庫中存儲任何內容(或由NHibernate生成的SQL查詢)。
我在做什麼錯?
你有訪問NHProfiler(如果不是爲什麼嗎?)?它向你展示了什麼? –
它顯示在未添加新參數的情況下執行的查詢。 – reallyJim
爲什麼不映射此屬性'公共虛擬字符串來源{得到{返回「我的應用程序的名稱」; } private set {}}'? – dotjoe