Q:

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 :
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. 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!!

A:

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.  Also noticed you can leave the method type still as CountryEF.
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. 

