Using the same database in multiple projects

I have 2 projects in my solution: the first is ClassLibrary, and the second is my application. I am using the Entity Framework (Code First method) with Sql Server Compact in a second project. The question is, how can I use the same database in my projects? I tried to move all my objects, DbContext and GenericRepository to my library, and also tried to set the connection string inside the code, but I continue to get the IInvalidOperationException:

The Entity Framework provider type "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer 'registered in the application configuration file for the ADO.NET provider with the invariant name" System.Data.SqlClient "failed to load. Make sure the name and that the assembly is available for the running application. For more details see http://go.microsoft.com/fwlink/?LinkId=260882 . information.

Here is my app.config:

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> </configSections> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework"> <parameters> <parameter value="System.Data.SqlServerCe.4.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" /> </providers> </entityFramework> <system.data> <DbProviderFactories> <remove invariant="System.Data.SqlServerCe.4.0" /> <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> </DbProviderFactories> </system.data> </configuration> 
+6
source share
3 answers

Try checking if the Entity Framework is referenced in the startup project, and if the Entity Framework configuration settings are also specified in the application configuration file for this project.

+5
source

The error seems to indicate the absence of a reference to the assembly. Open another project, make sure that one problem has all the same links.

Take a look at this similar question. Failed to load Entity Framework provider type?

+1
source

You seem to be using the factory default connection. Have you tried to define connection strings with the parameter and indication of the data source in the .sdf file?

 <connectionStrings> <add name="ConnectionStringName" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|\DatabaseFileName.sdf" /> </connectionStrings> 

If that doesn't work, I have another idea. You are logged in with Windows Authentication. Maybe it is not possible to make two parallel connections through the same credentials? I'm not sure, but I will try to check a different way than Windows authentication.

+1
source

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


All Articles