开发者

Entity framework entity class mapping with a plain .NET class

I have the following in Entity Framework.

Table - Country

Fields

  • List item
  • Country_ID
  • Dialing_Code
  • ISO_Alpha2
  • ISO_Alpha3
  • ISO_Full

I would like to map only selected fields from this entity model to m开发者_如何学Pythony domain class.

My domain model class is

public class DomainCountry
{
    public int Country_ID { get; set; }
    public string Dialing_Code { get; set; }
    public string ISO_3166_1_Alpha_2 { get; set; }
}

The following will work, however insert or update is not possible. In order to get insert or update we need to use ObjectSet<>, but it will not support in my case.

IQueryable<DomainCountry> countries =
    context.Countries.Select(
        c =>
        new DomainCountry
            {
                Country_ID = c.Country_Id,
                Dialing_Code = c.Dialing_Code,
                ISO_3166_1_Alpha_2 = c.ISO_3166_1_Alpha_2
            });

Is there a nice solution for this? It wound be really fantastic.

Ideally it will be kind of proxy class which will support all the futures however highly customizable.

That is, only the columns we want to expose to the outer world.


The term for "plain .NET classes" is POCO - plain old CLR objects (inspired by POJO, plain old Java objects).

Read this blog post series, it helped me a lot:

http://blogs.msdn.com/b/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx


I want to do the same thing. My goal is to build a WCF service that can use the same set of objects as the application I'm building by sharing a DLL and sending/receiving the same classes. Additionally, I also wanted to limit what fields are exposed. After thinking about this for a while it seems a user-defined cast might do the trick. Have a look to see if it works for you.

http://www.roque-patrick.com/windows/final/bbl0065.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜