Ler*_*ica 16 entity-framework ef-code-first visual-studio-2012 entity-framework-6
我正在玩EF不同的工作流程.昨天我做了一个新项目并且Entity Framework 6是第一个建议,Nuget所以我决定尝试一下,这也是一个非常小的项目完全用于学习目的所以我想这将是一个很好的经验,EF 6因为我一直在工作Ef 5.
我的应用程序基于Code First方法.解决方案的结构显示在打印屏幕中:

该项目CodeFirstClasses旨在持有我的Entites.为了简单起见,因为我遵循教程,我只使用一个课程,你可能会看到 - 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; }
}
Run Code Online (Sandbox Code Playgroud)
而其他类- 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!");
Run Code Online (Sandbox Code Playgroud)
到目前为止,与我以前没有什么不同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>
Run Code Online (Sandbox Code Playgroud)
从我的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>
Run Code Online (Sandbox Code Playgroud)
我发现的另一个可能的解决方案是: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正确设置我的应用程序.
配置架构已从版本5更改为6.如上所述,provider节点已替换为contexts节点.
我们的想法是您可以使用相同的提供程序单独配置提供程序而不是所有上下文.(这与能够在一个数据库中存在多个上下文同步.这曾经被称为多租户,但后来被重命名为更简洁)
| 归档时间: |
|
| 查看次数: |
26097 次 |
| 最近记录: |