【问题标题】:How to resolve Warning : The element 'entityFramework' has invalid child element 'providers'. List of possible elements expected: 'contexts'如何解决警告:元素“entityFramework”具有无效的子元素“提供者”。预期的可能元素列表:“上下文”
【发布时间】:2013-10-31 19:08:28
【问题描述】:

我在玩 EF 不同的工作流程。昨天我做了一个新项目,Entity Framework 6Nuget 的第一个建议,所以我决定试一试,这也是一个非常小的项目,完全用于学习目的,所以我想尝试@987654324 会是很好的体验@ 因为我大部分时间都在使用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; }

    }

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

您可以看到的另一个项目是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,但这不是 @987654339 的问题@。

我的研究将问题/解决方案的可能来源缩小到手动更改 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 对。

【问题讨论】:

    标签: entity-framework visual-studio-2012 ef-code-first entity-framework-6


    【解决方案1】:

    您可以从here 安装适用于 VS2012 的 EF6 Designer,它将更新验证配置文件的架构。

    【讨论】:

    • 正如 OP 所问 - 你知道我是否可以添加一些 xsd/xmlns 以始终获取本节的最新版本架构(无需安装这个大包)?
    • 其实是可以的。 EF 是开源的,因此您可以从 codeplex 获取必要的文件。将entityframework.codeplex.com/SourceControl/latest#src/EFTools/…entityframework.codeplex.com/SourceControl/latest#src/EFTools/… 复制到Program Files (x86)\Microsoft Visual Studio 11.0\Xml\Schemas 文件夹(不是它可能想要覆盖应该没问题的目录xml 文件——以防万一进行备份)。您可能需要重新启动 Visual Studio。
    • @sinelaw - 实际上 VS2012 的 EF6 包比 VS2012 随附的原始包要小得多。 AFAIR 原版约为 72MB,而新版约为 25MB。这是可能的,因为我们改变了处理 loc 东西的方式。因此,如果您将原始设计器替换为 EF6 设计器,您实际上可能会获得一些空间(并且配置将被修复)
    • 谢谢! .xsd 文件在不使用 VS 编辑配置文件时也很有用。拥有资源通常是一种享受,这里还有一个用例:)
    • 这不是 EF6 nuget 包的一部分,这对我来说似乎很奇怪。我猜它不能管理那些文件?
    【解决方案2】:

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

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

    【讨论】:

    • 感谢您的解释,但您能否提供一些使用context 节点的工作示例,我试图找到一个但没有成功。没有自定义,可以用一些通用的东西来替换我的代码并可以使用吗?
    • @Leron - 这里 msdn.microsoft.com/en-us/data/jj556606.aspx 是 EF 配置结构的描述。
    猜你喜欢
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    相关资源
    最近更新 更多