How to change the database - Schema used by Entity Framework (mysql database)?

后端 未结 2 537
北荒
北荒 2021-01-06 04:21

I use EntityFramework in a project to connect to a Mysql database. The provider is Devart Dot.connect.

This application needs to connect to a database using connexio

相关标签:
2条回答
  • 2021-01-06 04:57

    After spending one day on this issue, I finally came to understand that the problem was coming from the model.edmx file.

    In this file, you have one line per EntitySet. On each EntitySet element there is an attribute called schema. In case of SQL Server this attribute is set to the related table schema :

    EntitySet Name="annee_civile" EntityType="openemisModel.Store.annee_civile" store:Type="Tables" Schema="mydatabase" />

    If you provide the name of the Schema when constructiong you own EntityConnection, it seem that there is a conflict and that finally, the Schema defined in the edmx file will be used even if you specified another one in the connection parameters.

    The solution is simply to remove the name of the schema in the edmx file. THIS WORKS FOR MYSQL, probably not when connecting to a SQL server.

    EntitySet Name="annee_civile" EntityType="openemisModel.Store.annee_civile" store:Type="Tables" Schema="" />

    The EntityConnectionStringBuilder :

    string providedString = "User Id=xxxx;Password=xxx;Host=xxxx;Database=anydatabasename"; EntityConnectionStringBuilder entityConnBuilder = new EntityConnectionStringBuilder(); entityConnBuilder.Provider = "Devart.Data.MySql"; entityConnBuilder.Metadata = @"res:///OpenEmisModel.csdl|res:///OpenEmisModel.ssdl|res://*/OpenEmisModel.msl"; entityConnBuilder.ProviderConnectionString = providedString;

    The EntityConnection and the object context using it:

    EntityConnection entityConnexionEmis = new EntityConnection(entityConnBuilder.ConnectionString);

    objectcontextEntities testingContext = new objectcontextEntities(entityConnexionEmis);

    The software is now able to connect to any database name.

    Hope this helps.

    0 讨论(0)
  • 2021-01-06 05:04

    Reference the dll Devart.Data.MySql.Entity.EF6.dll in the project.

    Somewhere when your application is starting up and before database operations take place, add the following:

    var config = MySqlEntityProviderConfig.Instance;
    config.Workarounds.IgnoreSchemaName = true;
    

    You will need to reference:

    using Devart.Data.MySql.Entity.Configuration;
    
    0 讨论(0)
提交回复
热议问题