Visual Studio 2012中数据库优先实体框架模型的MVC4脚手架 [英] MVC4 scaffolding of database-first Entity Framework model in Visual Studio 2012

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

问题描述

我在Visual Studio 2012中遇到问题,试图添加一个带脚手架的MVC4控制器。理想情况下,我想在我的Web应用程序的单独程序集中使用Add New Item - ADO.NET Entity Data Model(即不是Code First)从数据库生成实体框架模型(edmx文件等)。但是,当我设置它并使用Add Controller时,使用Entity Framework指定具有读/写操作和视图的MVC控制器,并从DatabaseModel程序集中选择一个Model类和Data上下文类,弹出以下警告消息。 p>


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


无法修改类是有意义的,因为它在另一个程序集中,尽管在同一个解决方案中,通过T4自动生成,但查看Payforit4Entities的自动生成代码,DbSet属性已经很清楚了。 / p>


  public DbSet< Mno> Mnoes {get;组; 


我尝试过其他一些事情。将数据模型直接放在Web应用程序中

  • 将模型类更改为各种其他表格在数据库中,如果使用实体框架将Mno类

  • 将数据模型简化为简单的一个简单表

  • 电动工具Beta 2以逆向工程代码第一模型。这产生了一组新的错误。我可以看到为什么它是一个测试版。

  • 将ADO.NET数据模型代码生成策略从无到更改为基于ObjectContext创建数据模型而不是DbContext

  • 关闭多元化,所以属性名称是Mno而不是Mnoes

  • 它们都没有工作。唯一的工作是用手编写一个Code First DbContext派生类和POCO。巧合的是,我发现每个例子演示了MVC4脚手架使用这种数据模型。有没有什么地方说Code First是唯一可以使用MVC4脚手架的数据模型?有没有人设法在Visual Studio 2012中首先构建数据库(.edmx)数据模型?数据库很复杂,我宁愿坚持使用数据库的第一个策略。



    我可以看到,Code First对脚手架的区别是数据库第一模型。例如,前者具有保存由KeyAttribute指示的键的POCO属性,而后者将该信息保存在edmx模型文件中。这是实体框架电动工具中逆向工程功能的理由吗?我们预计将从edmx文件转移到逆向工程化的Code First模型,以便使用MVC4脚手架?如果是这样,我们是否希望在实体框架电动工具完成之前继续使用动态数据项目?

    解决方案

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



    参见这里:


    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天全站免登陆