How to prevent NHibernate from creating duplicate records for referenced objects
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.
精彩评论