6

I have 2 projects in my solution: the first one is a ClassLibrary and the second is my Application. I'm using Entity Framework (Code First method) with Sql Server Compact in my second project. The question is how can I use the same database in bith my projects? I've tried to move all my Entities, DbContext and GenericRepository to my Library and I also tried to set connection string inside the code, but I keep receiving IInvalidOperationException:

The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more 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>
Nobody One
  • 213
  • 4
  • 13

3 Answers3

5

Try to check if Entity Framework is referenced in the starting project and if Entity Framework configurations are set on application configuration file of that project as well.

Marco Alves
  • 2,776
  • 3
  • 23
  • 33
  • I've installed EF via Nuget for both my projects. I have config files in both projects (my config can be seen in my first post). – Nobody One Jun 06 '14 at 16:11
  • go to the bin folder of the application project, not class library. probably you won't find entity framework there, indicating either it isn't referenced or that it isn't marked to copy to output folder. – Marco Alves Jun 06 '14 at 16:14
  • Everything works fine in my project with the final application: database is generated successfully from my entities and I can work with this database via my GenericRepository. The problem is that I can't use it in Library project even if i create a EDMX model from this database in my library project. – Nobody One Jun 06 '14 at 16:17
  • However, your post really helped me: I was testing my Library Project via 3rd project for Unit-Testing and that Project had no necessary references. I'd like also to mention that i moved my DbContext and Entities to LibraryProject as well as GenericRepository so i don't need to create additional EDMX model on another project. – Nobody One Jun 06 '14 at 20:52
1

The error seems to indicate that there is an assembly reference missing. Open up the other project, make sure the one with the problem has all the same references.

Take a look at this similar question Entity Framework Provider type could not be loaded?

Community
  • 1
  • 1
user1666620
  • 4,800
  • 18
  • 27
  • I installed all my packages via NuGet package manager and I've already tried to use "var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;" as well. here is all my references: http://s17.postimg.org/f31dftcgv/snapshot1.png . Everything looks fine. – Nobody One Jun 06 '14 at 16:19
1

It seems you use Default Connection Factory. Did you try define connection strings with datasource parameter and indication on .sdf file?

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

If it won't work, I've got one more idea. You're logging in by Windows Authentication. Maybe it's not possible to make two parallel connections via the same credentials? I'm not sure, but try to check other way than windows authentication.

magos
  • 3,371
  • 4
  • 29
  • 54