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.
Here is simplified example of problem:
We have such DTOs:
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<DetailsType> 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;}
}
This entities are mapped to corresponding DB tables with the same fields.
- ID field of MainType is Primary Key;
- MainTypeID of DetailsType is Foreign Key to MainType table.
- SomeIdentityID of DetailsType is FK to some other entity that is not important for this sample.
- MainTypeID SomeIdentityID is complex primary key for DetailsType table.
I have graph of such objects (1 main and list details), and determined role of user who performs update operation. My task is:
- IF current user has role Doctor - update Doctor fields in Main object and all Details objects, insert new details objects.
- IF current user has role Nurse - update Nurse fields in Main object and all Details objects.
- save current date to Updated field
- save current user id to LastUpdateBy field
- do not modify Created field and any other field that are not updated by this role.
So for example if I have user with role Doctor I should do following:
- Update DoctorField1, DoctorField2, Updated, LastUpdateBy in MainObject
- Update DoctorDetail, Updated, LastUpdateBy in every details object
- DO NOT modify any other fields.
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.
How can I make Entity Framework to update only needed fields and ignore another.
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 "@#$%&@#$%&@#$%&@#$%&@#$%&", 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.
I will appreciate any ideas and thoughts about this problem.