【发布时间】:2013-10-31 19:08:28
【问题描述】:
我在玩 EF 不同的工作流程。昨天我做了一个新项目,Entity Framework 6 是Nuget 的第一个建议,所以我决定试一试,这也是一个非常小的项目,完全用于学习目的,所以我想尝试@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; }
}
还有其他类——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,但这不是 @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