2

我遇到了一个我不熟悉的错误。我试图谷歌没有成功。我在遇到此错误的地方编写了以下查询。

无法在 LINQ to Entities 查询中构造实体或复杂类型“MyWebProject.Models.UserDetail”。

查询:

UsersContext db = new UsersContext();
        var userdata = (from k in db.UserDetails
                        where k.UserId == WebSecurity.CurrentUserId
                        select new UserDetail()
                        {

                            FullName = k.FullName,
                            Email = k.Email,
                            About = k.About,
                            Link = k.Link,
                            UserSchool = new School()
                            {
                                SchoolId = k.UserSchool.SchoolId,
                                SchoolName = k.UserSchool.SchoolName
                            },
                            UserCourse = new Course()
                            {
                                CourseId=k.UserCourse.CourseId,
                                CourseName=k.UserCourse.CourseName
                            },
                            Country=k.Country
                        }).FirstOrDefault();

班级:

public class UserDetail
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public string FullName { get; set; }
    public string Link { get; set; }
    public bool? Verified { get; set; }

    public string Email { get; set; }

    public string About { get; set; }
    public School UserSchool { get; set; }
    public Course UserCourse { get; set; }

    public string Country { get; set; }

}
public class School
{
    public int SchoolId { get; set; }
    public string SchoolName { get; set; }
    public string Country { get; set; }
}
public class Course
{

    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public School School { get; set; }
}

知道出了什么问题吗??

4

4 回答 4

4

看起来这是由于您在查询中间创建复杂属性 School 和 Course 的方式。最好选择用户(删除选择转换),然后使用导航属性来访问这些对象,而不是手动构建它们。只要您使用外键建立适当的关系,导航就是为此而设计的。

UsersContext db = new UsersContext();

var userdata = (from k in db.UserDetails
    where k.UserId == WebSecurity.CurrentUserId})
    .FirstOrDefault();

// access navigation properties which will perform the joins on your behalf
// this also provides for lazy loading which would make it more effecient. (it wont load the school object until you need to access it)

userdata.School
userdata.Course

关于导航属性的 MSDN 文章:http: //msdn.microsoft.com/en-us/library/vstudio/bb738520 (v=vs.100).aspx

于 2012-12-10T20:35:07.453 回答
3

这应该给你你想要的。它将作为查询的一部分加载您的对象(而不依赖于延迟加载)。

UsersContext db = new UsersContext();
var userdata = db.UserDetails.Include(x => x.UserSchool)
                             .Include(x => x.UserCourse)
                             .Include(x => x.Country)
                             .Where(x => x.UserId == WebSecurity.CurrentUserId)
                             .FirstOrDefault();
于 2012-12-10T20:34:18.473 回答
0

我认为这是因为您的实体与您尝试创建的对象具有相同的名称。尝试重命名要返回的对象。如果您想返回与您的实体相同的类型,请尝试使用 .Include("relationshipname") 功能进行预加载。

于 2012-12-10T21:13:05.110 回答
0

下面给出了@Yakimych 的一个很好的答案。

您不能(也不应该)投影到映射的实体上。但是,您可以投影到匿名类型或 DTO:

public class ProductDTO
{
    public string Name { get; set; }
    // Other field you may need from the Product entity
}

您的方法将返回一个 DTO 列表。

public List<ProductDTO> GetProducts(int categoryID)
{
    return (from p in db.Products
            where p.CategoryID == categoryID
            select new ProductDTO { Name = p.Name }).ToList();
}

EF 中的映射实体基本上代表数据库表。如果你投影到一个映射实体上,你基本上做的是部分加载一个实体,这不是一个有效的状态。EF 不知道将来如何处理此类实体的更新(默认行为可能是用空值或对象中的任何内容覆盖未加载的字段)。这将是一个危险的操作,因为您可能会丢失数据库中的一些数据,因此不允许在 EF 中部分加载实体(或投影到映射实体上)。

有关更多详细信息,请转到以下链接: 无法在 LINQ to Entities 查询中构造实体

于 2012-12-11T02:16:27.933 回答