walter walter - 10 months ago 61
ASP.NET (C#) Question

How do I populate custom properties of a partial entity in Entity framework

say, I have a partial class that contains a custom property like this

public partial class Person {
public string ImagePath{get;set;}

and I'm using
to pull out data, where a stored procedure is executed that joins Person and Image by
Person.ImageId = Image.ImageId
and gets the ImagePath field from Image table.

This doesn't seem to work for partial class, but perfectly for inheritance.

However, I don't want to use inheritant class in this case, it possible for EF to be aware of the partial class's properties and populate them when executestorequery is executed?

Answer Source

No. If the Person class is mapped entity EF uses your mapping in EDMX file and it will populate only mapped properties because none of your custom properties from partial class is part of your mapping.

As a workaround create a new class PersonView which will not be mapped. Add all properties with the same name as columns in you result set and use it in ExecuteStoreQuery. In this case EF doesn't have mapping for the new class in EDMX so it will infer the simplest mapping - it will pair columns and properties by name.