Change Entity Framework Provider in Configuration

I was hoping you could switch data providers to / from SQL Server and SQL Server Compact Edition by simply changing the configuration. But it does not work and is looking at the EDMX file. I think I can understand why:

<edmx:StorageModels>
<Schema ... Provider="System.Data.SqlClient" ...

Is it possible to specify a provider in app.config or at runtime?

+3
source share
2 answers

The Storage model is tied to a specific provider, which will cause the Entity Framework to reject any DbConnection implementations that are not compatible with the specified provider.

Entity Framework, , StorageSchema, ModelSchema Mapping ( .edmx ). .edmx .ssdl,.csdl .msl , .ssdl SQL Server CE. .

:

+2

( ssdl ).

:

    var s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Model1.ssdl");
    var ssdlFilePath = "<some-dir>\file1.ssdl";
    using (var file = File.Create(ssdlFilePath))
    {
        StreamUtil.Copy(s, file);
    }
    var str = File.ReadAllText(ssdlFilePath);
    str = str.Replace("old provider token", "ProviderManifestToken=\"4.0\"");
    str = str.Replace("old provider type"", "Provider=\"System.Data.SqlServerCe.4.0\"");
    File.WriteAllText(ssdlFilePath, str);

app.config:

  <connectionStrings>
    <add name="Database2Entities" connectionString="metadata=res://*/Model1.csdl|<some-dir>\file1.ssdl|res://*/Model1.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=|DataDirectory|\Database1.sdf&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

)

+1

Source: https://habr.com/ru/post/1794283/


All Articles