【问题标题】:Why is compiler requesting reference to Entity Framework in User Interface project?为什么编译器要求在用户界面项目中引用实体框架?
【发布时间】:2013-10-18 22:11:30
【问题描述】:

我正在尝试学习如何创建一个使用 EF 和存储库模式的 Winform C# 应用程序。

根据我的理解,UI 层应该独立于实体框架(它通过存储库使用数据,它不应该关心是否有实体框架或任何其他数据提供者),但是如果 WinForms 项目是编译器会显示和错误没有对实体框架的引用(当我添加对 EF 的引用时,一切正常)

错误信息

找不到具有不变名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序。确保提供程序已在应用程序配置文件的“entityFramework”部分注册

为什么编译器需要我的 Winform 项目引用实体框架?

来自 Winforms 项目的代码

    private void MainForm_Load(object sender, EventArgs e)
    {
        //Customer is a POCO class
        using (var customers = new EfRepository<Customer>())
        {
            foreach (var c in customers.All())
            {
                CList.Items.Add(c.CustomerName);
            }
        }
    }

    private void SaveCustomerButton_Click(object sender, EventArgs e)
    {
        var newCustomer=new Customer();
        newCustomer.CustomerName = CName.Text;
        newCustomer.CustomerDescription = CDescription.Text;
        //newCustomer.
        using (var customers = new EfRepository<Customer>())
        {
            customers.Add(newCustomer);
            customers.Commit();
        }
    }

来自存储库项目的代码

public class EfRepository<T> : IRepository<T>, IDisposable where T : class//, IEntity<int>
{
    private FirstContext _ctx = new FirstContext();
    private DbSet<T> _set;

    public EfRepository()
    {
        //_ctx = _context;
        _set = _ctx.Set<T>();
    }
    public void Add(T newEntity)
    {
        _set.Add(newEntity);
    }
    public IQueryable<T> All()
    {
        return _set;
    }

上下文

public class FirstContext : BaseContext<FirstContext>
{
    public DbSet<Customer> Customers{ get; set; }
}

[编辑]

我添加了包含有关我的数据库的信息的 app.config(到我的 UI 项目),但它仅在我将实体框架添加到我的 UI 项目时才有效,如果我从我的 UI 项目存储库中删除连接到数据库的 EF 没有返回任何数据(也没有错误...)

这是我的 app.config 的内容:

<configuration>
  <configSections>
    <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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="RepositoryDb" connectionString="Data Source=.;Initial Catalog=RepositoryDb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

【问题讨论】:

  • 因为代码到处都在使用 EF?
  • 这不是编译器在抱怨。
  • @evanmcdonnal 您在“winforms 项目的代码”部分中在哪里看到 EF ???
  • Customer.Add()Customer.Commit() 都是 EF 调用。 Customer 是一个对象,它代表(我假设)你的数据库中的一个客户表。您正在向其中添加一行,然后调用Commit()。这些都是 EF 方法。
  • @evanmcdonnal Customer 是我的 POCO 类(在没有 EF 参考的域项目中!)并且 customer 是通用存储库的实例(当然有 EF 参考),存储库返回 IQueryable (也尝试过IEnumerable) 和 BindingList 而那些 Add 和 Commit 方法是 Repository 方法...抱歉命名混乱(“Customer” - POCO 对象,“customers” - Customer 类型的通用存储库实例)

标签: c# winforms entity-framework-6


【解决方案1】:

您的 GUI 项目不应该直接需要 EF 程序集是对的。当然,它仍然会通过您的存储库项目加载它们。

但默认情况下,应用程序仅使用 1 个配置文件。
所以你确实需要 app.config 中的数据库设置,就像错误告诉你的那样。

当您添加 EF 库时,配置文件中可能有一些更改。当您再次删除库时,它可能会继续工作。

【讨论】:

  • 我尝试了您的建议,但仍然没有成功使我的 UI 项目独立于实体框架。我编辑了我的帖子并添加了我的 app.config 文件的内容。我不再收到错误消息,但如果我从我的 UI 项目中删除实体框架,我不会从数据库中获取数据......也许你还有其他想法?
  • 你找到解决办法了吗?
【解决方案2】:

将配置文件放入主应用程序后,这就是解决我的问题

 public PSContext() : base() 
        {

            var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
        }

没有更多错误...

【讨论】:

  • 原因是 Visual Studio 会剥离所有引用的程序集,如果您不使用其中的任何代码。
猜你喜欢
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-13
  • 2017-03-16
相关资源
最近更新 更多