我可以射出我的查询由EF生成的模型的结果? [英] Can I project the result of my query to the model generated by EF?

查看:129
本文介绍了我可以射出我的查询由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;
        }


    }

在哪里COUNTRYEF是一个模型类由实体框架生成看起来像这样:

Where COUNTRYEF is a Model Class Generated by Entity Framework which looks like this :

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

    }

和上述溶液工作正常和良好的。但我真的想复制由实体框架生成的模型类,如公共部分类COUNTRYEF 来像公共类COUNTRYVM 。我真的不想做code重复。

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

    }

但我不能访问ID,说明和CurrencyEF在我的视图和控制器!

But then I cannot access the ID , Description and CurrencyEF in my Views and Controllers!!

推荐答案

实体框架不允许创建实体类里面的投影。东西硬做它来跟踪它应该被跟踪的实体。周围的工作我用。而不是拷贝和粘贴类只是继承它。

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

这在我还没有与其他任何版本中使用它EF 5.欺骗工作。也注意到你仍然可以保留方法类型CountryEF。

This tricked worked in EF 5. I haven't used it with any version else. Also noticed you can leave the method type still as CountryEF.

您可以创建一个T4为你创造的所有子类。此外,如果你需要用投影帮助,我相信AutoMapper现在支持IQueryable的。否则,你可以手工编写或与前presison树木的预测。

You could create a T4 to create all the subclasses for you. Also if you need help with the projections I believe AutoMapper now supports IQueryable. Otherwise you can write the projections by hand or with expresison trees.

这篇关于我可以射出我的查询由EF生成的模型的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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