Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在实体框架中填充部分实体的自定义属性_Asp.net_Entity Framework - Fatal编程技术网

Asp.net 如何在实体框架中填充部分实体的自定义属性

Asp.net 如何在实体框架中填充部分实体的自定义属性,asp.net,entity-framework,Asp.net,Entity Framework,比如说,我有一个分部类,它包含这样一个自定义属性 public partial class Person { public string ImagePath{get;set;} } 我使用ExecuteStoreQuery提取数据,其中执行一个存储过程,通过Person.ImageId=Image.ImageId连接Person和Image,并从Image表中获取ImagePath字段 这似乎不适用于分部类,但适用于继承 但是,我不想在这种情况下使用inheritant类,那么…EF是

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

public partial class Person {
    public string ImagePath{get;set;}
}
我使用
ExecuteStoreQuery
提取数据,其中执行一个存储过程,通过
Person.ImageId=Image.ImageId
连接Person和Image,并从Image表中获取ImagePath字段

这似乎不适用于分部类,但适用于继承


但是,我不想在这种情况下使用inheritant类,那么…EF是否可能知道分部类的属性,并在执行executestorequery时填充它们?

否。如果映射了
Person
类,则实体EF在EDMX文件中使用您的映射,并且它将只填充映射的属性,因为分部类中的自定义属性都不是您的属性的一部分映射


作为一种解决方法,创建一个新类
PersonView
,该类不会被映射。添加与结果集中的列同名的所有属性,并在
ExecuteStoreQuery
中使用。在这种情况下,EF在EDMX中没有新类的映射,因此它将推断出最简单的映射-它将按名称对列和属性进行配对。

我知道这有点旧,但我刚才碰到了这个问题并找到了解决方法,请参阅下面的方法:

public IList<Person> GetPerson()
{
    var mod = (from p in Person
              join i in Image on p.ImageId equals i.ImageId
              select new 
              {
                  p.Name,
                  i.ImageId,
                  i.ImagePath
              }).ToList();

     return (from m in mod
            select new Person
            {
                Name = m.Name,
                ImageId = m.ImageId,
                ImagePath = m.ImagePath
            }).ToList();
}
public IList GetPerson()
{
var mod=(从p到个人)
在p.ImageId上的图像中加入i等于i.ImageId
选择新的
{
p、 名字,
i、 ImageId,
i、 图像路径
}).ToList();
返回(在mod中从m开始)
选择新人
{
Name=m.Name,
ImageId=m.ImageId,
ImagePath=m.ImagePath
}).ToList();
}
公共部分类人员列表:人员
{
公共字符串ImagePath{get;set;}
}
列表myList=(
从p到db.Person
在p.ref_图像上的db图像中加入i等于i.id
选择新个人列表()
{
id=p.id,name=p.name,ImagePath=i.path
}
).ToList();
Console.Write(myList[0].id.ToString()+“,”+
myList[0].名称+”(“+myList[0].图像路径+”);

我也使用了继承!谢谢你,沃尔特!我觉得EF真的很差劲,但你提供了一个伟大的工作!
public partial class PersonList: Person
{
    public string ImagePath { get; set; }
}

List<PersonList> myList = (
    from p in db.Person
    join i in db.Images on p.ref_image equals i.id
    select new PersonList()
    {
        id = p.id, name = p.name, ImagePath = i.path
    }
).ToList<PersonList>();

Console.Write(myList[0].id.ToString() + ", " +
    myList[0].name + " (" + myList[0].ImagePath + ")");