如何正确安排使用SQLite数据库和Entity Framework的WPF项目,数据访问层在单独的dll中

时间:2015-11-26 23:13:56

标签: c# wpf entity-framework sqlite

我准备了连接到SQLite数据库的WPF应用程序。

我的应用程序没有组织成分层,我想将DAL - 数据访问层分离为单独的项目。

我遇到连接数据库的问题。

是否应在DAL dll app.config或WPF app app.config中定义连接? 我应该在哪里存储我的sqlite数据库文件?

你能举出好的练习例子吗?

我认为这种连接应该在DLL之外定义,但在这种情况下我遇到了定义该连接的问题。

3 个答案:

答案 0 :(得分:1)

实体框架将在主项目的App.config中查找SQLite连接字符串,因此您希望将其放在那里。就个人而言,我认为代码应该独立于连接字符串,因为您可能有不同的数据库副本以满足不同的需求。

如果您有测试(您应该),他们可能会指向与实际应用程序不同的测试数据库。数据库具有相同的结构,代码应该都表现相同,它们只有不同的连接字符串。

答案 1 :(得分:0)

我个人更喜欢将所有配置保存在一个文件中;但是,最终,它实际上取决于你如何看待正在使用的DAL dll。想想可能会发生什么变化以及如何使用模块。

例如:

  • 如果您有多个应用程序,并且您希望它们共享同一个数据库,那么将它放在DAL配置中会最有意义,就像连接字符串更改时一样,您只需要在一个地方更改它。 / LI>
  • 如果这是一个由多个不同数据库使用的界面,那么app配置会更有意义,因为您不希望DAL的更新推送到应用程序的选择之上。

答案 2 :(得分:0)

我有完全相同的配置:在单独的DLL中使用DAL的实体框架模型。我做的是我在DAL DLL项目中创建了一个app.config文件。此app.config文件包含EF使用SQLite提供程序所需的所有内容,但没有连接字符串。

这是DAL DLL的app.config:

<?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>

我认为这是DLL中所有需要它才能构建的。

主程序还有一个app.config并引用了DAL DLL项目。它的app.config定义了用于访问数据库的连接字符串(在我创建模型时由Entity Framework模型向导创建)。它还包含我的应用程序中所需的所有其他内容和其他内容。

以下是app.config的样子,删除了我所有应用程序的特定内容:

<?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>

主程序不进行数据库调用。每个数据库访问都通过DAL dll中的代码完成。

相关问题