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

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

问题描述

我目前正在从流畅的nHibernate迁移到ADO.Net实体框架4.

我有一个项目,包含我用于nHibernate映射的域模型(pocos)。我读到博客,可以使用我现有的域名模型与EF4,但没有看到它的例子。我看到了使用EF4的T4代码生成的例子,但是没有显示如何使用现有的域模型对象与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

Thanks Aiyaz

推荐答案

快速演练


  • 创建一个实体数据模型(.edmx),并清除edmx文件的自定义工具属性以防止生成代码

  • 在实体数据模型中创建与实体数据模型相同的实体,名称与你的域类。实体属性也应具有与域类相同的名称和类型

  • 创建从 ObjectContext 继承的类来公开实体(通常与.edmx文件相同的项目)

  • 在该类中,创建一个类型为 ObjectSet< 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

示例代码

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

这是关于它...

重要提示:如果您使用自动代理创建进行更改跟踪( ContextOptions.ProxyCreationEnabled ,默认情况下为true) ,您的域类的属性必须为虚拟。这是必要的,因为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天全站免登陆