简体   繁体   中英

How to correctly arrange WPF project that uses SQLite database with Entity Framework, and data access layer is in separate dll

I have prepared WPF app which connects to SQLite database.

My application is not organized into layered and I wanted to separate DAL - data access layer into separated project.

I have a problem with connection to database.

Should connection be defined in DAL dll app.config or in WPF app app.config? Where should I store my sqlite database file?

Could you give good practice example?

I think such connection should be defined outside DLL but in such case I have a problem with defining that connection.

Entity Framework will look in the App.config of your main project for the SQLite connection string, so you'll want to put it there. Personally, I think the code should be independent of the connection string, since you may have different database copies for different needs.

If you have tests (which you should), they will probably point to a different test database than the actual application. The databases have the same structure, and the code should all behave the same, they just have different connection strings.

I personally prefer to keep all the config in one file; but, ultimately, it really depends on how you see the DAL dll being used. Think what might change and how the modules might get used.

Ex:

  • If you have multiple applications and you want them all share the same database, maybe putting it in the DAL config would make the most sense as if/when the connection string changes, you only have to change it in one place.
  • If this is to be a interface used by multiple different databases, the app config would make more sense as you don't want updates to DAL to push on top of the application's choice.

I have the same exact configuration: Entity Framework model with a DAL in a separate DLL. What I did was I created an app.config file in the DAL DLL project. This app.config file has all of the stuff that is needed for EF to use the SQLite provider, but no connection string.

Here is the app.config for the DAL DLL:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SQLite" publicKeyToken="db937bc2d44ff139" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.97.0" newVersion="1.0.97.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    </DbProviderFactories>
  </system.data>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
</configuration>

I think this is all needed in the DLL for it to be able to be built.

The main program also has an app.config and references the DAL DLL project. Its app.config defines the connection string for accessing the database (which was created by the Entity Framework model wizard when I created the model). It also has all the other stuff and other things that are needed in my application.

Here's what that app.config looks like, with all of my app's specific stuff removed:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- More stuff here for my app in particular . . . -->
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <remove name="SQLiteConnection" />
    <remove name="EntitiesConnection" />
    <add name="SQLiteConnection" connectionString=". . ." />
    <add name="EntitiesConnection" connectionString=". . ." />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <system.data>
    <!--
        NOTE: The extra "remove" element below is to prevent the design-time
              support components within EF6 from selecting the legacy ADO.NET
              provider for SQLite (i.e. the one without any EF6 support).  It
              appears to only consider the first ADO.NET provider in the list
              within the resulting "app.config" or "web.config" file.
    -->
    <DbProviderFactories>
        <remove invariant="System.Data.SQLite" />
        <add invariant="System.Data.SQLite" name="SQLite Data Provider" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
        <remove invariant="System.Data.SQLite.EF6" />
        <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
</system.data>
<!-- Ohter stuff particular to my app -->
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SQLite" publicKeyToken="db937bc2d44ff139" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.97.0" newVersion="1.0.97.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

The main program makes no database calls. Every database access is done through the code in the DAL dll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM