实体框架实体类与普通 .NET 类的映射

发布于 2024-10-15 17:53:09 字数 1032 浏览 1 评论 0原文

我在实体框架中有以下内容。

表 - 国家/地区

字段

  • 列表项
  • Country_ID
  • Dialing_Code
  • ISO_Alpha2
  • ISO_Alpha3
  • ISO_Full

我想仅将此实体模型中的选定字段映射到我的域类。

我的域模型类是

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

以下内容可以工作,但是不可能插入或更新。为了插入或更新,我们需要使用 ObjectSet,但在我的情况下它不支持。

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

对此有一个好的解决方案吗?这真是太棒了。

理想情况下,它将是一种代理类,它将支持所有未来,无论高度可定制。

也就是说,只有我们想要向外部世界公开的列

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 my 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-10-22 17:53:09

“普通 .NET 类”的术语是 POCO - 普通的旧 CLR 对象(受到 POJO,普通的旧 Java 对象)。

阅读此博客文章系列,它对我帮助很大:

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

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

走野 2024-10-22 17:53:09

我想做同样的事情。我的目标是构建一个 WCF 服务,该服务可以通过共享 DLL 并发送/接收相同的类来使用与我正在构建的应用程序相同的对象集。此外,我还想限制暴露的字段。经过思考一段时间后,用户定义的强制转换似乎可以解决问题。看看它是否适合您。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文