riteshrao / ncommon

A framework for implementing commonly used design patterns when using Domain Driven Design
www.codeinsanity.com
218 stars 61 forks source link

Saving Objects Containing Attached Properties #13

Open crjackso opened 13 years ago

crjackso commented 13 years ago

I have a question regarding how to use NCommon (Entity Framework module) to save objects that have not been persisted before. However, the object I am trying to save has properties of complex types that have been added before. So, for example, let's say I have a Classroom object which has a one to many relationship with a Student object like so...

class Classroom { public virtual IEnumerable Students { get; set; } }

If the Classroom's Student collection has a Student object that already exists in the database, I can't call Save() on the IRepository (EFRepository) because Save() simply calls SaveChanges on the ObjectContext. Thus, I get "duplicate key exceptions" because EntityFramework is trying to add a Student object that already exists. So, is it possible to have a Save() method that will smartly handle objects that have yet to be persisted which contain properties that have been persisted?

skorunka commented 13 years ago

Save function is for saving new objects only. In case of modification, just use UnitOfWork(using(...){}), it takes care of updated properties. I have created an issue ticket for renaming Save function to something more appropriate.