开发者

How to prevent NHibernate from creating duplicate records for referenced objects

开发者 https://www.devze.com 2022-12-08 05:55 出处:网络
I\'m having trouble with saving an object into my database using NHibernate. My code looks like this: Image image = new Image { Url = \'test.jpg\' };

I'm having trouble with saving an object into my database using NHibernate.

My code looks like this:

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

I would like to prevent the image from be开发者_如何转开发ing created twice. Is there a standard NHibernate solution for that or do i have to create a custom Save() method for the Product class?


After a long search i found something about implementing the Equals() method for my entity classes so that NHibernate can compare them. Maybe this is the solution to my problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消