16

我正在玩 EF 不同的工作流程。昨天我做了一个新项目,Entity Framework 6是第一个建议,Nuget所以我决定试一试,这也是一个非常小的项目,完全用于学习目的,所以我想这将是很好的体验,EF 6因为我一直在与Ef 5.

我的应用程序基于Code First方法。解决方案的结构显示在打印屏幕中:

解决方案树

该项目CodeFirstClasses旨在容纳我的实体。为简单起见,并且因为我遵循教程,所以我只使用一个类,如您所见 - Customer.cs。我有:

public class RewardContext : DbContext
    { 
        //Specify the name of the base as Rewards
        public RewardContext() : base("Rewards")
        { 
        }

        //Create a database set for each data item
        public DbSet<Purchase> Purchases { get; set; }
        public DbSet<Customer> Customers { get; set; }

    }

还有其他的类——Purchase它们Customer是微不足道的,所以我不会在这里粘贴它们。

如您所见,另一个项目是Windows Forms只有一个表单和按钮的项目。在click按钮的事件中,我拥有将新记录添加到硬编码的两个实体的所有逻辑。这里只是其中的一部分:

    //some code...
    //Add the record and save it
    context.Customers.Add(newCustomer);
    context.Purchases.Add(newPurchase);
    context.SaveChanges();

    MessageBox.Show("Record Added!");

到目前为止,与我习惯的没有什么不同EF 5。我可以构建项目,我可以运行它,并且一切都按预期执行。但是我从标题中得到了这个警告:

Warning 1 The element 'entityFramework' has invalid child element 'providers'. List of possible elements expected: 'contexts'.即使我主要使用MS SQL Server Management Studio我已经注意到我无法从 IDE 管理我的连接/数据库 -Visual Studio 2012但这不是EF 5.

我的研究将问题/解决方案的可能来源缩小到手动更改App.config文件,但这是我没有太多经验的领域,尤其是当 IDE 处理它直到EF 6. 所以我会App.config为这个解决方案发布我的两个文件:

项目中的一个CodeFirstClasses

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <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>

从我的TestCodeFirst项目中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <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>

我发现的另一个可能的解决方案是:updating the xsd for "validating" EF config section in web/app.config file to recognize newly added EF6 elements我也不知道该怎么做。

即使当我打开MS SQL Server Management Studio我看到为该应用程序创建的数据库时,记录已保存并且通常它似乎可以工作,但我想解决此警告并了解如何根据EF 6正确设置我的应用程序。

4

2 回答 2

32

您可以从此处安装适用于 VS2012 的 EF6 设计器,它将更新验证配置文件的架构。

于 2013-10-22T22:39:14.173 回答
6

配置模式从版本 5 更改为 6。正如它所说,提供者节点已替换为上下文节点。

这个想法是您可以单独配置提供程序,而不是使用同一个提供程序的所有上下文。(这与能够在一个数据库中拥有多个上下文相一致。这曾经被称为多租户,但后来被重命名为更简洁)

于 2013-10-22T18:58:48.477 回答