如何在 Entity Framework 4 中查询实体

发布于 2024-09-01 02:49:03 字数 769 浏览 9 评论 0原文

在VS2008中,我认为是EF1.0,这个工作得很好。

string queryString = @"SELECT VALUE USERS FROM ProjectDBEntities.Users AS User 
            INNER JOIN ProjectDBEntities.Favorites AS F ON F.FavUserId = User.UserId
            WHERE F.UserId = " + 3 + " ORDER BY F.CreateDate DESC ";

        System.Data.Objects.ObjectQuery<User> usersQuery =
                new System.Data.Objects.ObjectQuery<User>(queryString, context).Include("Detail");

        //int count = usersQuery.Count();
        foreach (User result in usersQuery)
            Console.WriteLine("User Name: {0}", result.UserName);

VS2010 EF4 中的相同代码在 foreach 循环上崩溃,并出现以下错误:

查询的结果类型既不是 EntityType,也不是具有实体元素类型的 CollectionType。只能为具有这些结果类型之一的查询指定包含路径。

In VS2008, I think it is EF1.0, this works just fine.

string queryString = @"SELECT VALUE USERS FROM ProjectDBEntities.Users AS User 
            INNER JOIN ProjectDBEntities.Favorites AS F ON F.FavUserId = User.UserId
            WHERE F.UserId = " + 3 + " ORDER BY F.CreateDate DESC ";

        System.Data.Objects.ObjectQuery<User> usersQuery =
                new System.Data.Objects.ObjectQuery<User>(queryString, context).Include("Detail");

        //int count = usersQuery.Count();
        foreach (User result in usersQuery)
            Console.WriteLine("User Name: {0}", result.UserName);

Same code in VS2010 EF4 it crashes on the foreach loop with the following error:

The result type of the query is neither an EntityType nor a CollectionType with an entity element type. An Include path can only be specified for a query with one of these result types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2024-09-08 02:49:03
var q = from u in ProjectDBEntities.Users
        from f in u.Favorites
        where f.User.Id == 3
        orderby f.CreateDate desc;

我正在对您的实体模型/属性名称做出一些假设,因为您没有显示它,但这应该为您提供总体思路。

var q = from u in ProjectDBEntities.Users
        from f in u.Favorites
        where f.User.Id == 3
        orderby f.CreateDate desc;

I'm making some presumptions about your entity model/property names since you don't show it, but this should give you the general idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文