[英]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. 您可以将AdaptiveClient和AdaptiveClient.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组件的方法。
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.