Ef6: Access different types of database using the same entity model?

Created on 9 May 2017  路  5Comments  路  Source: dotnet/ef6

_1_. How can one use one entity model to connect multiple database provider like mssql, mysql,oracle ,mongodb etc.
_2_. Also what if we don't have entity defined in our model, is it still possible to perform CRUD operations on database table.
Found this on internet but not helpful ( references ).

PLEASE CHECK

https://www.codeproject.com/Articles/82017/Preparing-an-Entity-Framework-model-for-multi-prov https://www.codeproject.com/Tips/1062375/Entity-Framework-Multi-DB-Support

Please anyone help on this. @ajcvickers @shanselman @jongalloway @csharpfritz @AndriySvyryd @anpete @lajones @bricelam @moozzyk @maumar

closed-question

All 5 comments

Have you tried asking on StackOverflow.com? The community is typically very responsive and helpful when it comes to questions about how to use Entity Framework.

Had tried earlier no success on stackoverflow and asp.net forums.

Let me know if there is better solution to implement this using other than
entityframework

Thanks

@narendrasinghrathore Please try to be more specific about the questions you are asking. I'll try to give you some general answers as best I can with what you asked. For your first question, the approach to take depends on whether you are using the Code First or Database First approaches. (Check out the documentation for more info on these approaches.) With Code First, the application typically builds an EF model per database-provider in a transparent way. Your application code should remain the same, assuming that you only use features supported by all providers. For Database First with an EDMX, you will likely end up building a different EDMX models per database provider. There are ways to use the same CSDL and MSL, and only change the SSDL, but that gets more complicated.

For your second question, you can use the raw SQL APIs to make changes to anything in the database. See Raw SQL Queries in the documentation.

@ajcvickers I am using code first , By passing connection strings (_which are stored in web.config_ ) name to dbcontext , i am able to connect to mssql or mysql. But generating connection string dynamically doesn't seems to work, as ef dbcontext take mssql connector as defaultConnectionFactory. How i change the providerName. Generating connection string using EntityConnectionStringBuilder is for database first model( i found on internet ). Can you help me on this, how should i generate and change default provider so i can connect to database provider of my choice ( for now mssql, mysql ).

```C#
switch (providerName)
{
case "System.Data.SqlClient":
entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = providerName;
//ConfigurationManager.ConnectionStrings["mssql"].ConnectionString
break;

 case "MySql.Data.MySqlClient":
     entityBuilder = new EntityConnectionStringBuilder();
    entityBuilder.Provider = providerName;
    //ConfigurationManager.ConnectionStrings["mysql"].ConnectionString
 break;

 default:
 break;

}

switch (providerName)
{
case "System.Data.SqlClient":
conn = new SqlConnectionStringBuilder(entityBuilder.ProviderConnectionString)
{
UserID = databaseUserId,
Password = databasePassword,
DataSource = serverName,
IntegratedSecurity = false,
InitialCatalog = databaseName
}.ConnectionString;
break;

    case "MySql.Data.MySqlClient":
       conn = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder(entityBuilder.ProviderConnectionString)
       {
                    Server = serverName,
                    UserID = databaseUserId,
                    Password = databasePassword,
                    Database = databaseName,
                    PersistSecurityInfo = true
       }.ConnectionString;
       break;
       default:
       break;

}

```xml
            <DbProviderFactories>
            <remove invariant="MySql.Data.MySqlClient" />
            <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net                                                             Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,             Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
            </DbProviderFactories>

            <entityFramework>
            <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
            <parameters>
            <parameter value="mssqllocaldb" />
            </parameters>
            </defaultConnectionFactory>
            <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers>
            </entityFramework>

C# public ModelCodeFist(string connection) : base(connection) { }
Please let me know, if i missed something,
Link on stackoverflow, you can answer here also

http://stackoverflow.com/questions/43993918/dynamic-connection-string-for-entity-framework-code-first-for-multiple-database

http://stackoverflow.com/questions/43998212/dynamically-change-update-setdefaultconnectionfactory-and-setproviderservices-en
Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Colorfulmoose picture Colorfulmoose  路  4Comments

steamwings picture steamwings  路  7Comments

PhilPJL picture PhilPJL  路  7Comments

hayer picture hayer  路  6Comments

Elufimov picture Elufimov  路  7Comments