Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework LINQ to实体中不支持指定的类型成员“EntityKey”_Entity Framework_Linq To Entities_Entity - Fatal编程技术网

Entity framework LINQ to实体中不支持指定的类型成员“EntityKey”

Entity framework LINQ to实体中不支持指定的类型成员“EntityKey”,entity-framework,linq-to-entities,entity,Entity Framework,Linq To Entities,Entity,我有两个实体UserProfile和Agent,它们是一对多关系。我想通过提供userProfileEntityKey来执行查询以获取代理列表。当我运行它时,我得到了这样的结果:指定的类型成员“EntityKey”在LINQ to Entities错误中不受支持 public IQueryable<Agent> GetAgentListByUserProfile(EntityKey userProfileEntityKey) { ObjectQuery<Agent> ag

我有两个实体UserProfile和Agent,它们是一对多关系。我想通过提供userProfileEntityKey来执行查询以获取代理列表。当我运行它时,我得到了这样的结果:指定的类型成员“EntityKey”在LINQ to Entities错误中不受支持

public IQueryable<Agent> GetAgentListByUserProfile(EntityKey userProfileEntityKey)
{
ObjectQuery<Agent> agentObjects = this.DataContext.AgentSet;

IQueryable<Agent> resultQuery =
                    (from p in agentObjects
                     where p.UserProfile.EntityKey == userProfileEntityKey
                     select p);
    return resultQuery;
}
那么,正确的方法是什么呢?我是否使用p.UserProfile.UserId=UserId?如果是这样的话,它就不再是概念性的了。或者我应该写对象查询而不是LINQ查询?

试试这个

首先,重写实体对象中的Equals和GetHashCode方法。如果两个实体对象的类型相同且ID相同,则它们应该相等

接下来,使用此操作

public IQueryable<Agent> GetAgentListByUserProfile(UserProfile userProfile)
{
    var agentObjects = this.DataContext.AgentSet;
    var results = (from p in agentObjects
                   where p.UserProfile == userProfile
                   select p);
    return results;
}

我会选择更简单的路径p.UserProfile.UserId=UserId是您的解决方案

这是假设UserId是表的主键

按EntityKey搜索不会使其概念化。当您呈现一个以不同方式呈现db层的层时,就会出现概念性的部分

我是否误解了你对概念的看法