mục này được lái xe cho tôi điên ;-) tôi đang cố gắng để làm một truy vấn đơn giản khi tham gia hai bảngMVC 4. và Entity Framework Bảng Tham
tôi có như sau:
phương pháp Repository Lớp
public IQueryable<ADPerson> FindAll(string UserId)
{
return (from p in db.ADPerson
select p);
}
Trong điều khiển của tôi:
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,
};
tôi tiếp tục nhận được một e rror:
{"The specified LINQ expression contains references to queries that are associated with different contexts."}
Tôi cũng đã cố gắng thêm vào lớp kho:
phương pháp Repository Lớp
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;
}
là nó thực sự rất khó có thể làm một cách đơn giản kết hợp giữa hai bảng và cư biến trong Khuôn khổ thực thể
Cảm ơn
Đó có phải là lỗi tương tự bằng cách sử dụng phương pháp thứ hai? – mattytommo
Điểm tuyệt vời: Không có lỗi được trả về vào lần thứ hai là: Thực thể hoặc kiểu phức 'project.Models.ADPerson' không thể được xây dựng trong truy vấn LINQ to Entities. –
RE: lỗi thứ hai. Đó là bởi vì bạn không thể * project * vào một thực thể được ánh xạ. Bạn có thể chiếu truy vấn tới một đối tượng ẩn danh và sau đó ánh xạ nó tới thực thể ADPerson sau đó –