Different EF Property DataType than Storage Layer Possible?
I am putting together a WCF Data Service for PatientEntities using Entity Framework.
My solution needs to address these requirements:
- Property DateOfBirth of entity Patient is stored in SQL Server as string. It would be ideal if the entity class did not also use the "string" type but rather a DateTime type. (I would expect this to be possible since we're abstracting away from the storage layer). Where could a conversion mechanism be put in place that would convert to and from DateTime/string so that the entity and SQL Server are in sync?. I cannot change the storage layer's structure, so I have to work around it.
- WCF Data Services (Read-only, so no need for saving changes) need to be used since clients will be able to use LINQ expressions to consume the service. They can generate results based on any given query scenario they need and not be constrained by a single method such as GetPatient(int ID).
I've tried to use DTOs, but run into problem of mapping the ObjectContext to a DTO, I don't think that is theoretically possible...or too compli开发者_开发百科cated if it is.
I've tried to use Self Tracking Entities but they require the metadata from the .edmx file if I'm correct, and this isn't allowing a different property data type.
I also want to add customizations to my Entity getter methods so that a property "MRN" of type "string" needs to have .Replace("MR~", string.Empty) performed before it is returned. I can add this to the getter methods but the problem with that is Entity Framework will overwrite that next time it refreshes the entity classes. Is there a permanent place I can put these?
Should I use POCO instead? How would that work with WCF Data Services? Where would the service grab the metadata?
This is definitely possible. What you need to use is QueryView which lets you control how a given column maps to a property on your entity. for instance here is something u could do on patients entity.
<EntitySetMapping Name="Patients">
<QueryView>
select value conceptualnamespace.Patient(p.PatientId,
cast(p.DateOfBirth as Edm.DateTime),
replace(p.Name,'MR~','')
from entitycontainer.Patients as p
</QueryView>
</EntitySetMapping>
i cover more on this concept in my book. the recipe is called. 15-2. Mapping an Entity to Customized Parts of One or More Tables
精彩评论