简体   繁体   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

I have prepared WPF app which connects to SQLite database. 我已经准备了连接到SQLite数据库的WPF应用程序。

My application is not organized into layered and I wanted to separate DAL - data access layer into separated project. 我的应用程序没有组织成分层结构,我想将DAL(数据访问层)分离到单独的项目中。

I have a problem with connection to database. 我与数据库的连接有问题。

Should connection be defined in DAL dll app.config or in WPF app app.config? 是否应该在DAL dll app.config或WPF app app.config中定义连接? Where should I store my sqlite database file? 我应该在哪里存储我的sqlite数据库文件?

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. 我认为此类连接应在DLL外部定义,但在这种情况下,定义该连接存在问题。

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. 实体框架将在主项目的App.config中查找SQLite连接字符串,因此您需要将其放在此处。 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. 但最终,这实际上取决于您如何看待所使用的DAL dll。 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. 如果您有多个应用程序,并且希望它们都共享同一个数据库,则将其放在DAL配置中似乎是最有意义的,就好像/当连接字符串更改时,您只需要在一个位置进行更改即可。
  • 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. 如果这是多个不同数据库使用的接口,则应用程序配置会更有意义,因为您不希望对DAL进行更新以将应用程序置于首位。

I have the same exact configuration: Entity Framework model with a DAL in a separate DLL. 我有完全相同的配置:在单独的DLL中具有DAL的实体框架模型。 What I did was I created an app.config file in the DAL DLL project. 我所做的是在DAL DLL项目中创建了一个app.config文件。 This app.config file has all of the stuff that is needed for EF to use the SQLite provider, but no connection string. 这个app.config文件包含EF使用SQLite提供程序所需的所有内容,但没有连接字符串。

Here is the app.config for the DAL DLL: 这是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>

I think this is all needed in the DLL for it to be able to be built. 我认为这是DLL中所有必需的内容,以便可以进行构建。

The main program also has an app.config and references the DAL DLL project. 主程序还具有一个app.config并引用了DAL DLL项目。 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). 它的app.config定义了用于访问数据库的连接字符串(该字符串是在创建模型时由Entity Framework模型向导创建的)。 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: 这是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>

The main program makes no database calls. 主程序不进行数据库调用。 Every database access is done through the code in the DAL dll. 每个数据库访问都是通过DAL dll中的代码完成的。

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

相关问题 将实体框架EDMX类移动到单独的数据层DLL中 - Moving Entity Framework EDMX classes into a separate data layer DLL 在c#中分开的数据库实体层和数据访问层? - Separate DataBase Entity Layer and Data Access Layer in c#? 实体框架数据访问层项目的连接字符串 - Entity framework Data Access Layer project's connection string 在单独的数据访问和业务逻辑层中,我可以在业务层中使用Entity框架类吗? - In separate data access & business logic layer, can I use Entity framework classes in business layer? 具有独立实体框架数据层的Web API - Web API with separate entity framework data layer 使用 Entity Framework Core 数据库优先方法如何将我的实体与基础设施层分开? - With Entity Framework Core database first approach how do I separate my entity from infrastructure layer? 实体框架无法通过DLL访问数据库 - Entity Framework doesnt access via DLL to a Database 关于使用实体框架的数据访问层的建议 - Suggestion For Data Access Layer with Entity Framework 在具有数据访问层和App.Config的MVC4项目中是否存在新的Entity Framework 5的错误 - Is there a bug with the new Entity Framework 5 in an MVC4 Project with a Data Access Layer and App.Config 当使用实体框架作为数据访问层时,如何实现业务逻辑层? - How do you implement a business logic layer when using entity framework as data access layer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM