实体框架4与现有的域模型 [英] Entity Framework 4 with Existing Domain Model

查看:167
本文介绍了实体框架4与现有的域模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林目前正在研究从迁移连贯NHibernate到ADO.Net Entity Framework的4
我有一个包含域模型(波苏斯),我用了NHibernate的映射项目。香港专业教育学院读博客,它可以使用现有的域模型与EF4但香港专业教育学院看到它没有例子。香港专业教育学院T4 code一代看到的例子与EF4但还没有走到翻过这个例子展示了如何利用现有的域模型对象与EF4。即时通讯与EF4一个纽比,并希望看到关于如何让这个做了一些样品。

Im currently looking at migrating from fluent nHibernate to ADO.Net Entity Framework 4.
I have a project containing the domain model (pocos) which I was using for nHibernate mappings. Ive read in blogs that it is possible to use my existing domain model with EF4 but ive seen no examples of it. Ive seen examples of T4 code generation with EF4 but havent come accross an example which shows how to use existing domain model objects with EF4. Im a newby with EF4 and would like to see some samples on how to get this done.

谢谢 Aiyaz

推荐答案

快速演练:

  • 创建一个实体数据模型(的.edmx)在Visual Studio中,并清除EDMX文件的自定义工具属性设置为prevent code一代
  • 在使用相同的名称作为您的域名类的实体数据模型创建实体。实体的属性也应该有相同的名称和类型,在域类
  • 创建一个类从的ObjectContext 继承揭露实体(通常在同一个项目中的.edmx文件)
  • 在类中,创建类型的属性对象集< TEntity> 对于每次实体
  • Create an entity data model (.edmx) in Visual Studio, and clear the "custom tool" property of the edmx file to prevent code generation
  • Create the entities in your entity data model with the same names as your domain classes. The entity properties should also have the same names and types as in the domain classes
  • Create a class inherited from ObjectContext to expose the entities (typically in the same project as the .edmx file)
  • In that class, create a property of type ObjectSet<TEntity> for each of you entities

样品code:

public class SalesContext : ObjectContext
{
    public SalesContext(string connectionString, string defaultContainerName)
        : base(connectionString, defaultContainerName)
    {
        this.Customers = CreateObjectSet<Customer>();
        this.Products = CreateObjectSet<Product>();
        this.Orders = CreateObjectSet<Order>();
        this.OrderDetails = CreateObjectSet<OrderDetail>();
    }

    public ObjectSet<Customer> Customers { get; private set; }
    public ObjectSet<Product> Products { get; private set; }
    public ObjectSet<Order> Orders { get; private set; }
    public ObjectSet<OrderDetail> OrderDetails { get; private set; }
}

这就是它......

That's about it...

重要提示:如果您使用自动代理创建的变化跟踪( ContextOptions.ProxyCreationEnabled ,默认情况下是如此),属性你的域类的必须是虚。这是必要的,因为通过EF 4.0生成的代理将覆盖它们来实现更改跟踪。

Important notice : if you use the automatic proxy creation for change tracking (ContextOptions.ProxyCreationEnabled, which is true by default), the properties of your domain classes must be virtual. This is necessary because the proxies generated by EF 4.0 will override them to implement change tracking.

如果你不想使用自动代理的创建,您将需要处理的更改跟踪自己。请参见这个MSDN页细节

If you don't want to use automatic proxy creation, you will need to handle change tracking yourself. See this MSDN page for details

这篇关于实体框架4与现有的域模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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