MVC 4和Entity Framework表加入
此产品是推动我疯了;-) 我试图做一个简单的查询,将两个表MVC 4和Entity Framework表加入
我有以下几点:
仓储类方法
public IQueryable<ADPerson> FindAll(string UserId)
{
return (from p in db.ADPerson
select p);
}
在我的控制器:
var ADPersonList = from o in ADPersonDB.FindAll(GetUserId())
join c in MSDNTypeDB.FindAll(GetUserId()) on o.MsdnTypeId equals c.MsdnTypeId
select new ADPerson()
{
AdPersonId = o.AdPersonId,
SamAccountName = o.SamAccountName,
Description = o.Description,
DisplayName = o.DisplayName,
UserPrincipalName = o.UserPrincipalName,
Enabled = o.Enabled,
LastUpdated = o.LastUpdated,
OnlineAssetTag = o.OnlineAssetTag,
MsdnTypeId = o.MsdnTypeId,
MsdnSubscription = c.MsdnTypeDescription,
};
我不断收到一封RROR:
{"The specified LINQ expression contains references to queries that are associated with different contexts."}
我也尝试添加到存储库类:
仓储类方法
public IQueryable<ADPerson> FindAll(string UserId)
{
//return (from p in db.ADPerson
// select p);
var query = from o in db.ADPerson
join c in db.MsdnTypes on o.MsdnTypeId equals c.MsdnTypeId
select new ADPerson()
{
AdPersonId = o.AdPersonId,
SamAccountName = o.SamAccountName,
Description = o.Description,
DisplayName = o.DisplayName,
UserPrincipalName = o.UserPrincipalName,
Enabled = o.Enabled,
LastUpdated = o.LastUpdated,
OnlineAssetTag = o.OnlineAssetTag,
MsdnTypeId = o.MsdnTypeId,
MsdnSubscription = c.MsdnTypeDescription,
};
return query;
}
真的就那么难做到两个表之间的简单连接,并填充变量实体框架
谢谢
项目到一个匿名对象
var query = from o in db.ADPerson
join c in db.MsdnTypes on o.MsdnTypeId equals c.MsdnTypeId
select new
{
AdPersonId = o.AdPersonId,
SamAccountName = o.SamAccountName,
Description = o.Description,
DisplayName = o.DisplayName,
UserPrincipalName = o.UserPrincipalName,
Enabled = o.Enabled,
LastUpdated = o.LastUpdated,
OnlineAssetTag = o.OnlineAssetTag,
MsdnTypeId = o.MsdnTypeId,
MsdnSubscription = c.MsdnTypeDescription,
};
然后映射回你的实体
foreach (var item in query)
{
var adPerson = new ADPerson
{
AdPersonId = item.AdPersonId,
SamAccountName = item.SamAccountName,
Description = item.Description,
DisplayName = item.DisplayName,
UserPrincipalName = item.UserPrincipalName,
Enabled = item.Enabled,
LastUpdated = item.LastUpdated,
OnlineAssetTag = item.OnlineAssetTag,
MsdnTypeId = item.MsdnTypeId,
MsdnSubscription = item.MsdnTypeDescription,
{
}
第一个var查询失败是否有假设选择新? – 2013-03-14 19:50:46
@DavidCostelloe是的,对不起。编辑回答 – 2013-03-14 19:52:50
获取错误“无效的列名'MsdnSubscription”字段 – 2013-03-14 20:50:18
public IQueryable<ADPerson> FindAll(string UserId)
{
// return (from p in db.ADPerson
// select p);
List<ADPerson> lst = new List<ADPerson>();
var query = from o in db.ADPerson
join c in db.MsdnTypes on o.MsdnTypeId equals c.MsdnTypeId
select new
{
AdPersonId = o.AdPersonId,
SamAccountName = o.SamAccountName,
Description = o.Description,
DisplayName = o.DisplayName,
UserPrincipalName = o.UserPrincipalName,
Enabled = o.Enabled,
LastUpdated = o.LastUpdated,
OnlineAssetTag = o.OnlineAssetTag,
MsdnTypeId = o.MsdnTypeId,
MsdnSubscription = c.MsdnTypeDescription
};
foreach (var item in query)
{
var adPerson = new ADPerson()
{
AdPersonId = item.AdPersonId,
SamAccountName = item.SamAccountName,
Description = item.Description,
DisplayName = item.DisplayName,
UserPrincipalName = item.UserPrincipalName,
Enabled = item.Enabled,
LastUpdated = item.LastUpdated,
OnlineAssetTag = item.OnlineAssetTag,
MsdnTypeId = item.MsdnTypeId,
MsdnSubscription = item.MsdnSubscription
};
lst.Add(adPerson);
}
return lst.AsQueryable();
}
它可能是你有一个尾随逗号在“MsdnSubscription = c.MsdnTypeDescription”,也在第二个 – eburgos 2013-03-14 20:11:07
谢谢发现它是支柱{和};几乎在那里:-) – 2013-03-14 20:32:25
是否使用第二种方法同样的错误? – mattytommo 2013-03-14 18:04:20
优秀点:否第二个返回的错误是:实体或复杂类型'project.Models.ADPerson'不能在LINQ to Entities查询中构造。 – 2013-03-14 18:06:22
RE:第二个错误。那是因为你不能*投影到映射的实体上。您可以将查询投影到匿名对象,然后将其映射到ADPerson实体 – 2013-03-14 18:09:27