繁体   English   中英

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

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

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

我的应用程序没有组织成分层结构,我想将DAL(数据访问层)分离到单独的项目中。

我与数据库的连接有问题。

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

你能举个好例子吗?

我认为此类连接应在DLL外部定义,但在这种情况下,定义该连接存在问题。

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

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

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

例如:

  • 如果您有多个应用程序,并且希望它们都共享同一个数据库,则将其放在DAL配置中似乎是最有意义的,就好像/当连接字符串更改时,您只需要在一个位置进行更改即可。
  • 如果这是多个不同数据库使用的接口,则应用程序配置会更有意义,因为您不希望对DAL进行更新以将应用程序置于首位。

我有完全相同的配置:在单独的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中的代码完成的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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