我有一个使用 Entity Framework 获取数据的ASP.NET MVC应用程序。

我需要先将实体转换为模型,然后再将其传递给 View 。投影可能非常复杂,但为了简单起见:

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

可以在像这样的 Controller 中使用:
return View(Repository.Users.ToModel().ToList());

非常好。但是,如果我想在另一个投影中使用该投影怎么办?例子:
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类型的)。

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

最佳答案

这是实际的操作(在一个简单的测试应用程序中),只能选择请求的字段:

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);
        }
    }
}

要测试它而不实际创建数据库:
var blogPostsQuery = (
    from p in context.BlogPosts
    where p.Title.StartsWith("a")
    select p).ToModel();
Console.WriteLine(((ObjectQuery)blogPostQuery).ToTraceString());

关于asp.net-mvc - 如何在 Entity Framework 中重用投影?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11978130/

10-13 09:31