我如何填充实体框架的部分实体的自定义属性 [英] How do I populate custom properties of a partial entity in Entity framework

查看:142
本文介绍了我如何填充实体框架的部分实体的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有一个包含这样的自定义属性的分部类

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

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

和我使用 ExecuteStoreQuery<人> 拉出来的数据,在执行一个存储过程,加入的的和的图片的由 Person.ImageId = Image.ImageId 图片的得到的的ImagePath 的字段的表。

and I'm using ExecuteStoreQuery<Person> 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.

不过,我不希望使用inheritant类在这种情况下,所以......有可能是EF要注意局部类的属性,并执行executestorequery当填充它们?

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

推荐答案

没有。如果类映射实体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.

这篇关于我如何填充实体框架的部分实体的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆