1
我在使用NHibernate將對象保存到數據庫時遇到問題。如何防止NHibernate爲參考對象創建重複記錄
我的代碼如下所示:
Image image = new Image { Url = 'test.jpg' };
Product product1 = new Product { Name = 'MyProduct', Image = image };
Product product2 = new Product { Name = 'MyProduct2', Image = image };
MySession.Save(product1); // This also creates an Image record
MySession.Save(product2); // This creates another Image record even tho the data is exactly the same
我想防止創建兩次圖像。 是否有標準的NHibernate解決方案呢,還是我必須爲產品類創建一個自定義的Save()方法?
請發佈您的映射請 –
產品分類上的流利映射如下: Map(x => x.Name); 引用(x => x.Image).Cascade.SaveUpdate(); –
你有沒有嘗試過這個沒有自動初始化? product1.Image = image –