此產品是推動我瘋了;-) 我試圖做一個簡單的查詢,將兩個表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;
}
真的就那麼難做到兩個表之間的簡單連接,並填充變量實體框架
謝謝
是否使用第二種方法同樣的錯誤? – 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