且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何填充实体框架的部分实体的自定义属性

更新时间:2023-02-25 21:11:45

没有。如果类映射实体EF使用您的映射EDMX文件,它将填充只映射属性,因为没有一个从局部类的自定义属性是你映射的一部分。

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.

作为一种变通方法创建一个新类 PersonView ,不会被映射。使用相同的名称添加的所有属性,在你列的结果集,并在 ExecuteStoreQuery 使用它。在这种情况下,EF不具有用于在EDMX新类映射所以它会推断最简单的映射 - 它将通过名称配对列和属性

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.