Visual Studio 2013 和实体框架 [英] Visual Studio 2013 and Entity Framework

查看:23
本文介绍了Visual Studio 2013 和实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VS 2013 和 SQL Svr 2012 并尝试使用 EF 来填充 gridview.由于这是一个非常简单的测试,我只有一个带有键和几个数据字段的表.创建模型的过程似乎工作正常 - 它在设计器视图中按预期显示,在 app_code 中创建文件,使用 2 个 dll 创建 bin 文件夹,并且使用连接字符串和其他条目更新配置文件.

I'm using VS 2013 and SQL Svr 2012 and trying use EF to populate a gridview. As this is a very simple test, I have just one table with a key and a couple of data fields. The process of creating the model seems to work fine - it shows as expected in the designer view, the files are created in app_code, a bin folder is created with 2 dlls, and the config file is updated with a connection string and other entries.

当我尝试选择命名连接"时,我收到错误无法加载指定的元数据资源",它谈到重建项目以获取程序集.

When I try to select the "named connection" I get the error "Unable to load the specified metadata resource" and it talks about rebuilding the project to get the assemblies.

我花了几个小时阅读其他类似的案例,但我无法理解:

I've spent hours reading other cases like this and I can't understand:

  1. 为什么在 VS 2010 从来没有问题时会发生这种情况

  1. Why this is happening when it was never a problem with VS 2010

如何修复连接字符串以指向它想要的资源.我对关于完全限定程序集名称的所有技术建议完全迷失了 - 我尝试的任何方法都不起作用.

How do I fix the connection string to point to the resources it wants. I'm totally lost with all the technical advice about fully qualified assembly names - nothing I try works.

如何设置一些参数来强制资源位于正确的位置?我读过的所有建议似乎都与 VS 2013 不太相关

How do I set some parameter to force the resources to be in the proper place? All the advice I've read doesn't seem to relate very well with VS 2013

我需要决定是否应该只卸载 VS 2013 并使用 VS 2012.目前,我使用 VS 2010 和 ASP 4.0,我想转移到 ASP 4.5 并且通常跟上 Microsoft 技术.但是,我没有处理有问题的 VS 版本的技能水平.在我看来,这个 EF 执行可能是由于我的计算机上的错误或一些奇怪的损坏 - 我试图弄清楚是哪种情况.我在过去没有这些工具的干净机器上安装了 VS 2013 Express 和 SQL Svr 2012.

I need to decide if I should just uninstall VS 2013 and use VS 2012. Currently, I'm using VS 2010 with ASP 4.0 and I'd like to move to ASP 4.5 and generally keep up with Microsoft technology. However, I don't have the skill level to deal with a VS release that is buggy. It seems to me that this EF execise could be due to a either a bug or some odd corruption on my computer - I'm trying to figure out which is the case. I installed VS 2013 Express and SQL Svr 2012 on a clean machine that had none of these tools in the past.

我真的很想看到非常熟悉 Visual Studio 的人尝试复制我的问题,因为这样做只需要几分钟:

I'd really like to see someone who is very familiar with Visual Studio try to replicate my problem as it only takes a few minutes to do so:

  1. 在 MS SQL Server 中,创建一个带有一个表的小型数据库,该表具有一个主键和几个数据字段,并填充一些测试数据 - 使用 TestTable1 将其称为 TinyDB.

  1. In MS SQL Server, create a tiny DB with one table that has a primary key and a couple of data fields and populate with some test data - Call it TinyDB with TestTable1.

使用 VS 2013 Express for Web,使用 VB 创建一个带有ASP 空网站"选项的新网站".

Using VS 2013 Express for Web, create an "New Web Site" with the option for "ASP Empty Web Site" using VB.

添加一个包含ADO.Net 实体数据模型"的 ASP App_Code 文件夹,并将名称保留为模型".

Add an ASP App_Code Folder containing an "ADO.Net Entity Data Model" and leave the name as "Model".

从数据库生成模型并为TinyDB"建立新连接"并使用实体框架 6.0,然后选择TestTable1".注意命名空间是TinyDBModel".单击完成"将在 App_Code、其他文件夹和更新 web.config 中生成几个项目.

Generate the model from the DB and make a "New Connection" for "TinyDB" and using Entity Framework 6.0 and then select "TestTable1". Note the namespace is "TinyDBModel". Clicking "Finish" will generate several items in App_Code, other folders and also update web.config.

添加一个名为 default.aspx 的 Web 表单并将一个 gridview 拖到它上面.在gridview的设计模式下,选择新数据源",然后选择实体"选项(注意出现在default.aspx中的EntityDataSource1的名称).单击下一步并在命名连接"下选择TinyDBEntities" - 这是我收到错误无法加载指定的元数据资源"的地方.

Add a web form called default.aspx and drag a gridview onto it. In design mode for the gridview, select "new data source" and then the "Entity" option (note name of EntityDataSource1 which appears in default.aspx). Click next and under "named connection" select "TinyDBEntities" - this is where mine gets the error "Unable to load the specified metadata resource".

请注意,VS 2010 中的这个确切练习可以完美运行,并生成显示正确数据的网格.

Note that this exact exercise in VS 2010 works perfectly and produces a grid with the correct data displayed.

推荐答案

遗憾的是,EntityDataSource 控件不适用于最新版本的实体框架 EF6.希望该控件在 VS 或 EF 的未来更新版本中得到更新,但目前您不能同时使用两者.

Unfortunately, the EntityDataSource control does not work with the latest version of Entity Framework, EF6. Hopefully the control gets an update in a future update version of VS or EF, but for now you can't use the two together.

有一些解决方案/替代方案:

There are a few solutions / alternatives:

  1. 退回到 EF 5.为此,请从 App_Code 文件夹中删除 .edmx 和 .tt 文件,从 Bin 文件夹中删除 EF 程序集,并从 web.config 中删除对 EF 的引用.然后重建站点,添加新的 ADO.NET 实体模型并在选择您的版本"对话框中选择版本 5.然后从 App_Code 文件夹中删除 .tt 文件,打开 EDMX diagam,右键单击一个空白点,然后在属性网格上将代码生成策略设置为 Legacy ObjectContext.然后再次重建,它应该可以工作了.

  1. Drop back to EF 5. In order to do this, delete the .edmx and .tt files from the App_Code folder, delete the EF assemblies from the Bin folder, and remove the references to EF from web.config. Then rebuild the site, add a new ADO.NET Entity Model and choose version 5 in the Choose Your Version dialog. Then delete the .tt files from the App_Code folder, open the EDMX diagam, right-click an empty spot and on the Properties Grid set Code Generation Strategy to Legacy ObjectContext. Then rebuild again, and it should work.

使用模型绑定.这确实是推荐的解决方案.Microsoft/EF 团队建议人们远离 EntityDataSource 控件,仅将其用于动态数据网站,模型绑定是首选解决方案.它使用起来非常干净和直接,并且可以让您对查询和对象更新进行大量控制.有关模型绑定的更多信息,请查看:http://www.asp.net/web-forms/tutorials/data-access/model-binding/retrieving-data

Use Model Binding. This is really the recommended solution. The Microsoft / EF team recommends people to stay away from the EntityDataSource control and only use it for Dynamic Data web sites, Model Binding is the preferred solultion. It's pretty clean and straight forward to use, and gives you a lot of control over your queries and object updates. For more info about Model Binding, check out: http://www.asp.net/web-forms/tutorials/data-access/model-binding/retrieving-data

这篇关于Visual Studio 2013 和实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆