简体   繁体   English

我可以将查询结果投影到 EF 生成的模型吗?

[英]Can I project the result of my query to the model generated by EF?

I have a method something like below :我有一个类似下面的方法:

 public IEnumerable<CountryEF> GetAllCountry( )
    {


        using (Context context  = new Context() )
        {

            List<CountryEF> countries = context.COUNTRY.Select(c =>

                 new CountryEF()
                 {
                     ID = c.ID,
                     Description = c.DESCRIPTION,
                     CURRENCYEF = c.CURRENCYEF
                 }

            ).ToList<CountryEF>();

            return countries;
        }


    }

Where COUNTRYEF is a Model Class Generated by Entity Framework which looks like this :其中 COUNTRYEF 是由实体框架生成的模型类,如下所示:

public partial class COUNTRYEF
{
    public COUNTRYEF()
    {

    }

    public string ID { get; set; }
    public string DESCRIPTION { get; set; }
    public virtual CURRENCY CURRENCYEF { get; set; }

}

When ever I do this I get Exception.每当我这样做时,我都会得到异常。 So as a fallback I have to create another class which is just a copy-paste of above class like this below :因此,作为后备,我必须创建另一个类,它只是上面类的复制粘贴,如下所示:

    public class COUNTRYVM //view model class
    {
        public COUNTRYVM()
        {

        }

        public string ID { get; set; }
        public string DESCRIPTION { get; set; }
        public virtual CURRENCY CURRENCYEF { get; set; }

    }

And then my query becomes like this :然后我的查询变成这样:

    public IEnumerable<CountryVM> GetAllCountry( )
    {


        using (Context context  = new Context() )
        {

            List<CountryVM> countries = context.COUNTRY.Select(c =>

                 new CountryVM()
                 {
                     ID = c.ID,
                     Description = c.DESCRIPTION,
                     CURRENCYEF = c.CURRENCYEF
                 }

            ).ToList<CountryVM>();

            return countries;
        }

    }

And the above solution works fine and good.上述解决方案运行良好。 But Do I really want to replicate the Model classes generated by Entity Framework like public partial class COUNTRYEF to something like public class COUNTRYVM .但是我真的想将实体框架生成的模型类(如public partial class COUNTRYEFpublic class COUNTRYVM I really don't want to do code duplication.我真的不想做代码重复。

What are the possible solutions for this ?对此有哪些可能的解决方案? Anonymous properties also dont.匿名属性也没有。 I had tried the below solution :我曾尝试以下解决方案:

   public IEnumerable GetAllCountry( )
    {


        using (Context context  = new Context() )
        {

            var countries = context.COUNTRY.Select(c =>

                 new  //Anonymous Projection 
                 {
                     ID = c.ID,
                     Description = c.DESCRIPTION,
                     CURRENCYEF = c.CURRENCYEF
                 }

            ).ToList();

            return countries;
        }

    }

But then I cannot access the ID , Description and CurrencyEF in my Views and Controllers!!但是后来我无法在我的视图和控制器中访问 ID、描述和 CurrencyEF !!

Entity Framework doesn't allow creating classes of the Entity inside it's projection.实体框架不允许在其投影内创建实体的类。 Something to do with hard for it to track which entities it should be tracking.它很难跟踪它应该跟踪哪些实体。 The work around I use.我使用的解决方法。 Instead of copy and pasting the class just subclass it.而不是复制和粘贴类只是子类化它。

public partial class CountryEFSub : CountryEF
{
}


public IEnumerable<CountryEF> GetAllCountry( )
{

    using (Context context  = new Context() )
    {

        return context.COUNTRY.AsNoTracking().Select(c =>

             new CountryEFSub()
             {
                 ID = c.ID,
                 Description = c.DESCRIPTION,
                 CURRENCYEF = c.CURRENCYEF
             }

        ).AsEnumerable();
    }
}

This tricked worked in EF 5. I haven't used it with any version else.这在 EF 5 中有效。我没有在其他任何版本中使用它。 Also noticed you can leave the method type still as CountryEF.还注意到您可以将方法类型保留为 CountryEF。

You could create a T4 to create all the subclasses for you.您可以创建一个 T4 来为您创建所有子类。 Also if you need help with the projections I believe AutoMapper now supports IQueryable.另外,如果您需要投影方面的帮助,我相信 AutoMapper 现在支持 IQueryable。 Otherwise you can write the projections by hand or with expresison trees.否则,您可以手动或使用表达树来编写​​预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM