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

查看:106
本文介绍了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并使用VS2012。目前,我将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中,创建一个小型DB,其中一个表具有一个主键和几个数据字段,并填充一些测试数据-使用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.

添加包含以下内容的ASP App_Code文件夹 ADO.Net实体数据模型,并将名称保留为模型。

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

从数据库生成模型并为 TinyDB并使用Entity Framework 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的设计模式下,选择新数据源,然后选择实体选项(注意EntityDataSource1的名称,该名称出现在default.aspx中)。单击下一步,然后在命名连接下选择 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控件不适用于最新版本的Entity Framework 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. 拖放回EF5。为此,请从App_Code文件夹中删除.edmx和.tt文件。 ,从Bin文件夹中删除EF程序集,然后从web.config中删除对EF的引用。然后重建站点,添加一个新的ADO.NET实体模型,然后在选择您的版本对话框中选择版本5。然后从App_Code文件夹中删除.tt文件,打开EDMX diagam,右键单击一个空白点,然后在Properties Grid上将Code Generation Strategy设置为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控件,仅将其用于Dynamic Data网站,模型绑定是首选解决方案。它非常干净而且易于使用,并且可以让您对查询和对象更新进行很多控制。有关模型绑定的更多信息,请查看: 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天全站免登陆