实体框架实体类与普通 .NET 类的映射
我在实体框架中有以下内容。
表 - 国家/地区
字段
- 列表项
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“普通 .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
我想做同样的事情。我的目标是构建一个 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