简体   繁体   English

多个数据库提供者的一个实体框架模型

[英]One Entity Framework Model for Multiple Database Providers

I have used Entity Framework to generate models for database before. 我以前使用Entity Framework为数据库生成模型。 The thing is that Entity Framework generate the model for a specific provider (SQL Server, Oracle, etc ..). 问题是Entity Framework为特定的提供者(SQL Server,Oracle等)生成模型。 How can I generate a model that may work with many providers. 如何生成可与许多提供程序一起使用的模型。

I thought about handcrafting my own Data Access Layer using the DbProviderFactory class. 我想过使用DbProviderFactory类手工制作我自己的数据访问层。 But building the model from scratch involves a lot of work this is why I was wondering if I could generate an Entity Framework Model which works with more than one provider?!!! 但是从头开始构建模型涉及很多工作,这就是为什么我想知道我是否可以生成一个与多个提供者一起工作的实体框架模型?

Yes, you can use ONE DbContext and target multiple database providers! 是的,您可以使用ONE DbContext并定位多个数据库提供程序!

How can I generate a model that may work with many providers. 如何生成可与许多提供程序一起使用的模型。

Entity Framework will generate provider-specific SQL for you. 实体框架将为您生成特定于提供程序的SQL。 The real problem you have to solve is how to tell EF what provider to use when you have to make provider-specific calls on your own ie call provider-specific stored procs etc. You also have to deal with the issue of provider-specific migrations, database initializers, and provider-specific integration testing. 您必须解决的真正问题是,当您必须自己进行特定于提供程序的调用(即调用提供程序特定的存储过程等)时,如何告知EF要使用的提供程序。您还必须处理特定于提供程序的迁移问题,数据库初始化程序和特定于提供程序的集成测试。

You can use AdaptiveClient and AdaptiveClient.EntityFrameworkCore for all the challenges above. 您可以将AdaptiveClientAdaptiveClient.EntityFrameworkCore用于上述所有挑战。

In fewest words, AdaptiveClient allows you key your connection strings to provider-specific and transport-specific components in your application. 用最少的词来说,AdaptiveClient允许您将连接字符串键入应用程序中特定于提供程序的特定于传输的组件。

When a specific connection string is selected (ie MSSQL or MySQL) AdaptiveClient uses Autofac to resolve the correct components for you. 当选择特定的连接字符串(即MSSQL或MySQL)时,AdaptiveClient使用Autofac为您解析正确的组件。

All you need to do is register your provider/transport specific components using keys that you define. 您需要做的就是使用您定义的密钥注册提供者/传输特定组件。 AdaptiveClient provides a registration helper that makes this a very simple process since it provides methods that are specific to Entity Framework components. AdaptiveClient提供了一个注册助手,使其成为一个非常简单的过程,因为它提供了特定于Entity Framework组件的方法。

  • Use SOLID design principals to construct your service layer 使用SOLID设计原则构建服务层
  • Create a service layer that is scalable and loosely coupled 创建可伸缩且松散耦合的服务层
  • Create services that are granular and testable 创建细粒度且可测试的服务
  • Create provider-specific migrations 创建特定于提供程序的迁移
  • Create provider-specific database initializers 创建特定于提供程序的数据库初始化
  • Easily drop-and-recreate your database for integration testing 轻松删除并重新创建数据库以进行集成测试
  • Easily inject a single client that provides access to your entire service layer 轻松注入一个客户端,提供对整个服务层的访问
  • Use a service manifest (facade) to call any service from within any other service 使用服务清单(facade)从任何其他服务中调用任何服务

Download the complete working Zamagon Demo . 下载完整的Zamagon演示版 The demo illustrates migrations, database initializers, drop-and-recreate scenarios for integration testing and every bullet point in the list above. 该演示演示了迁移,数据库初始化程序,集成测试的删除和重新创建方案以及上面列表中的每个项目符号点。

AdaptiveClient is available as a nuget package . AdaptiveClient可用作nuget包

AdaptiveClient.EntityFrameworkCore is also available as a nuget package . AdaptiveClient.EntityFrameworkCore也可用作nuget包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架核心-一种用于多个查询的模型 - Entity Framework Core - one model for multiple queries 具有多种模型的实体框架(不同的提供者) - Entity Framework with multiple models (different providers) Entity Framework Core 2.1 - 多个提供者 - Entity Framework Core 2.1 - Multiple Providers 实体框架中的一个事务中有多个数据库 - Multiple database in one transaction in Entity Framework 实体框架数据库模型 - Entity Framework Database Model 实体框架代码的动态连接字符串优先在运行时EF6(mssql,mysql,oracle)用于多个数据库提供程序 - Dynamic connection string for entity framework code first for multiple database providers at runtime EF6 ( mssql, mysql, oracle ) 在多个应用程序之间共享一个Entity Framework 4数据模型的最佳方式是什么(意味着同时访问一个数据库)? - What's the best way to share one Entity Framework 4 data model among multiple applications (meant to access one database simultaneously)? Entity Framework 4 从具有多个模式的模型生成数据库 - Entity Framework 4 Generate Database From Model With Multiple Schemas 实体框架:一种用于具有不同结构的多个表的模型 - Entity Framework: One model for multiple tables with non identical structures Protobuf和实体框架数据库模型 - Protobuf and Entity Framework DataBase Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM