具有现有域模型的实体框架 4

发布于 2024-09-04 09:44:17 字数 248 浏览 1 评论 0原文

我目前正在考虑从 Fluent nHibernate 迁移到 ADO.Net Entity Framework 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.

Thanks
Aiyaz

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

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

发布评论

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

评论(1

烟火散人牵绊 2024-09-11 09:44:17

快速演练:

  • 在 Visual Studio 中创建实体数据模型 (.edmx),并清除 edmx 文件的“自定义工具”属性以防止代码生成 使用
  • 相同的实体数据模型创建实体名称作为您的域类。实体属性还应具有与域类中相同的名称和类型
  • 创建一个从 ObjectContext 继承的类以公开实体(通常与 .edmx 文件位于同一项目中)
  • 在该类中,创建每个实体的 ObjectSet 类型的属性

示例代码:

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),域类的属性必须是 virtual。这是必要的,因为 EF 4.0 生成的代理将覆盖它们以实现更改跟踪。

如果您不想使用自动代理创建,则需要自己处理更改跟踪。有关详细信息,请参阅此 MSDN 页面

Quick walkthrough :

  • 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

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

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.

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

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