Note that there are some explanatory texts on larger screens.

plurals
  1. POPartial update of entity objects with EF 4
    text
    copied!<p>I'm implementing DAL and BL layers of application. It is hosted as WCF service, and EF 4 is used as ORM. We have role based security layer and business rule that only part of object can be updated by some particular role.</p> <p>Here is simplified example of problem:</p> <p>We have such DTOs:</p> <pre><code>MainType { public Guid ID { get; set; } public String DoctorField1 { get; set; } public String DoctorField2 { get; set; } public String NurseField1 { get; set; } public String NurseField2 { get; set; } public DateTime Created {get; set;} public DateTime Updated {get; set;} public Guid LastUpdateBy {get; set;} public List&lt;DetailsType&gt; Details { get; set; } } DetailsType { public Guid MainTypeID { get; set; } public Guid SomeIdentityID { get; set; } public String DoctorDetail { get; set; } public String NurseDetail { get; set; } public DateTime Created {get; set;} public DateTime Updated {get; set;} public Guid LastUpdateBy {get; set;} } </code></pre> <p>This entities are mapped to corresponding DB tables with the same fields.</p> <ul> <li>ID field of MainType is Primary Key;</li> <li>MainTypeID of DetailsType is Foreign Key to MainType table.</li> <li>SomeIdentityID of DetailsType is FK to some other entity that is not important for this sample.</li> <li>MainTypeID SomeIdentityID is complex primary key for DetailsType table.</li> </ul> <p>I have graph of such objects (1 main and list details), and determined role of user who performs update operation. My task is:</p> <ul> <li>IF current user has role Doctor - update Doctor fields in Main object and all Details objects, insert new details objects.</li> <li>IF current user has role Nurse - update Nurse fields in Main object and all Details objects.</li> <li>save current date to Updated field</li> <li>save current user id to LastUpdateBy field</li> <li>do not modify Created field and any other field that are not updated by this role.</li> </ul> <p>So for example if I have user with role Doctor I should do following:</p> <ul> <li>Update DoctorField1, DoctorField2, Updated, LastUpdateBy in MainObject</li> <li>Update DoctorDetail, Updated, LastUpdateBy in every details object</li> <li>DO NOT modify any other fields.</li> </ul> <p>Currently we have implementation that reads full graph for MainObject, makes necessary modifications and saves in back to DB. This solution works too slow and I need to find a way to improve it. Currently I know clearly how to do that by RAW SQL, but this will be my solution in case nothing else will help.</p> <p>How can I make Entity Framework to update only needed fields and ignore another.</p> <p>I have some successful results with ApplyOriginalValues and ApplyCurrentValues methods for String fields. Idea was to assign some fictive value to property in both objects, for example string "@#$%&amp;@#$%&amp;@#$%&amp;@#$%&amp;@#$%&amp;", and EF then treats them as not modified properties during saving changes. However this trick does not work with Boolean, Int32 and Decimal values. I should use some simple approach to all objects.</p> <p>I will appreciate any ideas and thoughts about this problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload