在Visual Studio 2012的数据库,第一个实体框架模型MVC4脚手架 [英] MVC4 scaffolding of database-first Entity Framework model in Visual Studio 2012

查看:437
本文介绍了在Visual Studio 2012的数据库,第一个实体框架模型MVC4脚手架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Visual Studio 2012的问题,尝试添加一个MVC4控制器脚手架。我非常希望有使用添加新项目数据库中生成一个实体框架模型(EDMX文件等) - 在从我的Web应用程序中一个单独的程序ADO.NET实体数据模型(即没有code在前)。然而,当我设置和使用添加控制器,指定MVC控制器,具有读/写操作和视图,使用实体框架,并选择从我DatabaseModel装配模型类和数据上下文类以下警告信息弹出。


  

Ifl.Payforit4.DatabaseModel.Mno'不是指定的一部分
  Ifl.Payforit4.DatabaseModel.Payforit4Entities类,而
  Ifl.Payforit4.DatabaseModel.Payforit4Entities类不能
  修改为DbSet属性添加到
  它。 (例如,'Ifl.Payforit4.DatabaseModel.Payforit4Entities'
  类可能在编译的程序集。)


不能够修改类是有道理的,因为它在另一个程序集,虽然在同一个解决方案,并通过T4自动生成的,但看自动生成的code为Payforit4Entities了DbSet属性很清楚地存在了。


 公共DbSet<二氧化锰> Mnoes {搞定;组; }


我已经尝试了一些其他的东西。


  1. 直接把数据模型在Web应用程序

  2. 改变模型类的各种数据库中的其他表中的情况下存在与二氧化锰类的问题

  3. 降低数据模型只是一个单一的,简单的表

  4. 使用实体框架电动工具测试版2,以反向工程code首先模型。这产生了一组新的错误。我可以看到为什么它是一个测试版。

  5. 从没有改变ADO.NET数据模型code生成策略为默认创建基于ObjectContext的,而不是一个的DbContext数据模型

  6. 关闭生成复数所以属性名称是二氧化锰代替Mnoes

他们没有工作。该工作做的唯一的事情就是写一个code。通过先用手的DbContext派生类和POCO。巧合的是,我发现每个实例演示MVC4脚手架使用这种数据模型。是不是有什么地方,说code首先是,与MVC4脚手架工程数据模型的唯一排序?有没有人设法先脚手架数据库在Visual Studio 2012(的.edmx)数据模型?该数据库是足够复杂,我宁愿与数据库第一战略坚持下去。

我可以看到,有必须是在code的脚手架一定的差异首先对数据库的首款车型。例如,前者具有POCO属性保持由KeyAttribute表示的密钥,而后者保持在ED​​MX模型文件的信息。这是在实体框架电动工具逆向工程功能的基本原理?难道我们预计从EDMX文件移开,以反向工程code首款车型,以利用MVC4脚手架?如果是这样,我们的预期进行使用动态数据项目,直到实体框架电动工具完成?


解决方案

的技巧是先编译您​​的解决方案,然后在上下文类手动即可。不要从下拉列表中选择它,只需要输入自己的类名,它会神奇的工作; - )

在这里看到:<一href=\"http://jefferytay.word$p$pss.com/2013/02/19/asp-net-mvc4-how-to-use-a-database-first-ef-in-a-mvc-controller/\">ASP.NET MVC4-如何使用数据库首先EF在MVC控制器

I'm having problems with Visual Studio 2012, trying to add an MVC4 Controller with scaffolding. Ideally I would like to have an Entity Framework model (edmx file, etc) generated from a database using Add New Item - ADO.NET Entity Data Model (i.e. not Code First) in a separate assembly from my web application. However, when I set this up and use Add Controller, specifying "MVC controller with read/write actions and views, using Entity Framework" and choosing a Model class and Data context class from my DatabaseModel assembly the following alert message pops up.

'Ifl.Payforit4.DatabaseModel.Mno' is not part of the specified 'Ifl.Payforit4.DatabaseModel.Payforit4Entities' class, and the 'Ifl.Payforit4.DatabaseModel.Payforit4Entities' class could not be modified to add a 'DbSet' property to it. (For example, the 'Ifl.Payforit4.DatabaseModel.Payforit4Entities' class might be in a compiled assembly.)

Not being able to modify the class makes sense since it is in another assembly, although in the same solution, and auto-generated via T4 but looking at the auto-generated code for Payforit4Entities the 'DbSet' property is quite clearly there already.

    public DbSet<Mno> Mnoes { get; set; }

I've tried a number of other things.

  1. putting the data model directly in the web application
  2. changing the model class to a variety of other tables in the database in case there is a problem with the Mno class
  3. reducing the data model to just a single, simple table
  4. using the Entity Framework Power Tools Beta 2 to reverse engineer a Code First model. This generated a new set of errors. I can see why it is a beta.
  5. changing the ADO.NET Data Model Code Generation Strategy from None to Default to create a data model based on ObjectContext rather than DbContext
  6. Turning off pluralisation so the property name is Mno instead of Mnoes

None of them worked. The only thing that did work was writing a Code First DbContext-derived class and POCO by hand. Coincidentally every example I've found that demonstrates MVC4 scaffolding uses this sort of data model. Is there something somewhere that says Code First is the only sort of data model that works with MVC4 scaffolding? Has anyone managed to scaffold a database first (.edmx) data model in Visual Studio 2012? The database is complex enough that I'd rather stick with a database first strategy.

I can see that there would have to be some differences in the scaffolding of Code First versus Database First models. For example, the former has the POCO property holding the key indicated by a KeyAttribute whereas the latter holds that information in the edmx model files. Is this the rationale for the reverse engineer feature in Entity Framework Power Tools? Are we expected to move away from edmx files to reverse engineered Code First models in order to use MVC4 scaffolding? If so, are we expected to carry on using Dynamic Data projects until the Entity Framework Power Tools are finished?

解决方案

The trick is to first compile your solution and then type in the context class manually. Don't choose it from the Dropdown list, just type in the class name by yourself and it will magically work ;-)

See here: ASP.NET MVC4– How to use a Database First EF in a MVC controller

这篇关于在Visual Studio 2012的数据库,第一个实体框架模型MVC4脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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