One Entity Framework Model for Multiple Database Providers
Asked Answered
C

1

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?!!!

Cabaret answered 24/1, 2014 at 21:27 Comment(3)
Did you solved this problem? If yes, maybe you can share it.Aisha
Unfortunately I did not. So, I went with writing my own DAL which was limited to the providers I have included at the time.Cabaret
Did you try the Stackoverflow Dapper and its Extensions. Dapper is a lightweight ORM which is fast and simple enough.Acuity
G
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.

Gynaecomastia answered 4/12, 2018 at 0:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.