如何在实体框架中重用投影? [英] How to reuse projections in Entity Framework?

查看:63
本文介绍了如何在实体框架中重用投影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用实体框架获取数据的ASP.NET MVC应用程序.

I have an ASP.NET MVC application which uses Entity Framework to get data.

我需要先将实体转换为模型,然后再将其传递给视图.投影可能非常复杂,但要保持简单:

I need to transform Entites to Models before passing them to View. Projections can be very complex, but to keep it simple:

public static IQueryable<UserModel> ToModel(this IQueryable<User> users)
{
    return from user in users
           select new UserModel
           {
               Name = user.Name,
               Email = user.Email,
           };
}

这可以在像这样的控制器中使用:

This can be used in a controller like this:

return View(Repository.Users.ToModel().ToList());

很好.但是,如果我想在另一个投影中使用该投影怎么办?示例:

Very good. But what if I want to use this projection inside another one? Example:

public static IQueryable<BlogPostModel> ToModel(this IQueryable<BlogPost> blogs)
{
    return from blogs in blogs
           select new BlogPostModel
           {
               Title = blog.Title,
               Authors = blog.Authors.AsQueryable().ToModel(), // (entities are POCOs)
               // This does not work, because EF does not understand method ToModel().
           };
}

(假设博客的作者人数可以超过一个,并且该博客的类型为User).

(let's suppose blog can have more then one author and it is of type User).

我可以以某种方式分离投影并在其他投影中重复使用吗?

Can I somehow separate the projections and reuse them inside another ones?

推荐答案

以下是实际工作的内容(在简单的测试应用程序中),它只能选择请求的字段:

Here's something that actually works (in a simple test application) to only select the requested fields:

namespace Entities
{
    public class BlogPost
    {
        public virtual int Id { get; set; }
        public virtual string Title { get; set; }
        public virtual DateTime Created { get; set; }
        public virtual ICollection<User> Authors { get; set; }
    }

    public class User
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Email { get; set; }
        public virtual byte[] Password { get; set; }
        public virtual ICollection<BlogPost> BlogPosts { get; set; }
    }
}

namespace Models
{
    public class BlogPostModel
    {
        public string Title { get; set; }
        public IEnumerable<UserModel> Authors { get; set; }
    }

    public class UserModel
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }

    public static class BlogPostModelExtensions
    {
        public static readonly Expression<Func<BlogPost, BlogPostModel>> ToModelConverterExpression =
            p =>
            new BlogPostModel
            {
                Title = p.Title,
                Authors = p.Authors.AsQueryable().Select(UserModelExtensions.ToModelConverterExpression),
            };

        public static readonly Func<BlogPost, BlogPostModel> ToModelConverterFunction = ToModelConverterExpression.Compile();

        public static IQueryable<BlogPostModel> ToModel(this IQueryable<BlogPost> blogPosts)
        {
            return blogPosts.Select(ToModelConverterExpression);
        }

        public static IEnumerable<BlogPostModel> ToModel(this IEnumerable<BlogPost> blogPosts)
        {
            return blogPosts.Select(ToModelConverterFunction);
        }
    }

    public static class UserModelExtensions
    {
        public static readonly Expression<Func<User, UserModel>> ToModelConverterExpression =
            u =>
            new UserModel
            {
                Name = u.Name,
                Email = u.Email,
            };

        public static readonly Func<User, UserModel> ToModelConverterFunction = ToModelConverterExpression.Compile();

        public static IQueryable<UserModel> ToModel(this IQueryable<User> users)
        {
            return users.Select(ToModelConverterExpression);
        }

        public static IEnumerable<UserModel> ToModel(this IEnumerable<User> users)
        {
            return users.Select(ToModelConverterFunction);
        }
    }
}

要在不实际创建数据库的情况下对其进行测试:

To test it without actually creating a database:

var blogPostsQuery = (
    from p in context.BlogPosts
    where p.Title.StartsWith("a")
    select p).ToModel();
Console.WriteLine(((ObjectQuery)blogPostQuery).ToTraceString());

这篇关于如何在实体框架中重用投影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆