本文介绍了如何申请在FK财产预测在实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此我现在可以到我的投影逻辑存储在一个防爆pression 并使用它的另一个预测中。

Using this answer I am now able to store my projection logic in an Expression and use it inside another projections.

然而,当我开始实施这一计算策略在我的解决方案,我发现,我是不能够使用存储的防爆pression 上一个导航财产是一个单一的FK(不是集合)。

However, when I started to implement this approch in my solution, I found out, that I am not able to use the stored Expression on a Navigation property which is a single FK (not a collection).

以下code演示了这个问题:

The following code demonstrates this issue:

namespace Entities
{
    public class BlogPost
    {
        public virtual int BlogPostId { get; set; }
        public virtual string Title { get; set; }

        public virtual string NotUsed { get; set; }

        public virtual User Author { get; set; }
    }

    public class User
    {
        public virtual int UserId { get; set; }
        public virtual string Name { get; set; }
        public virtual string NotUsed { get; set; }

        public virtual ICollection<BlogPost> BlogPosts { get; set; }
    }
}

namespace Models
{
    public class BlogPostModel
    {
        public string Title { get; set; }
        public UserModel Author { get; set; }
    }

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

    public static class BlogPostModelExtensions
    {
        public static readonly Expression<Func<BlogPost, BlogPostModel>> ToModelConverterExpression =
            p =>
            new BlogPostModel
            {
                Title = p.Title,
                Author = null, //Problem!
                // I need to convert User (p.Author) to UserModel using UserModelExtensions.ToModelConverterExpression
            };
    }

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

是否有可能使用单FK导航属性转换为模型防爆pression

推荐答案

这是目前可能在过于复杂的方式:

This is currently possible in an overly complicated manner:

p =>
new BlogPostModel
{
    ...,
    Author = new[] { p }.AsQueryable().Select(UserModelExtensions.ToModelConverterExpression).FirstOrDefault()
}

不过,生成的SQL,而正确的,是不必要的复杂和缓慢。据我所知,目前还没有任何办法直接获取你想要的,但我一直在寻找同样的事情,我有证据的概念补丁的开源待EF - 6.0我打算提交列入,请参见和<一个href=\"https://entityframework.$c$cplex.com/SourceControl/network/forks/hvdijk/entityframework/changeset/eb7219a484c3\"相对=nofollow>更改。

这篇关于如何申请在FK财产预测在实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 20:35