6

I have used Entity Framework to generate models for database before. The thing is that Entity Framework generate the model for a specific provider (SQL Server, Oracle, etc ..). 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. 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?!!!

Saleh Omar
  • 739
  • 2
  • 8
  • 29

1 Answers1

1

Yes, you can use ONE DbContext and target multiple database providers!

How can I generate a model that may work with many providers.

Entity Framework will generate provider-specific SQL for you. 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 i.e. 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.

You can use AdaptiveClient and AdaptiveClient.EntityFrameworkCore for all the challenges above.

In fewest words, AdaptiveClient allows you key your connection strings to provider-specific and transport-specific components in your application.

When a specific connection string is selected (i.e. MSSQL or MySQL) AdaptiveClient uses Autofac to resolve the correct components for you.

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.

  • Use SOLID design principals to construct your service layer
  • 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

Download the complete working Zamagon Demo. 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.EntityFrameworkCore is also available as a nuget package.