2012 Visual Studio中数据库第一实体框架模型的MVC4脚手架

时间:2021-12-19 09:51:53

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.

我在Visual Studio 2012上遇到了问题,我想添加一个带支架的MVC4控制器。理想情况下,我希望使用Add New Item - ADO从数据库生成一个实体框架模型(edmx文件等)。在与我的web应用程序的单独程序集中的NET实体数据模型(即不是先编写代码)。但是,当我设置它并使用Add Controller时,指定“具有读/写操作和视图的MVC控制器,使用实体框架”,并从我的DatabaseModel程序集中选择一个模型类和数据上下文类时,将弹出以下警报消息。

'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.)

“Ifl.Payforit4.DatabaseModel。Mno不是指定的“Ifl.Payforit4.DatabaseModel”的一部分。Payforit4Entities类,以及Ifl.Payforit4.DatabaseModel。不能修改payforit4entity的类来添加“DbSet”属性。(例如,“Ifl.Payforit4.DatabaseModel。Payforit4Entities类可能位于已编译的程序集中。

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.

不能修改类是有意义的,因为它在另一个程序集中,尽管在相同的解决方案中,并且通过T4自动生成,但是要查看Payforit4Entities的自动生成代码,“DbSet”属性显然已经存在了。

    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. 将数据模型直接放到web应用程序中。
  3. changing the model class to a variety of other tables in the database in case there is a problem with the Mno class
  4. 如果Mno类有问题,则将模型类更改为数据库中的其他表
  5. reducing the data model to just a single, simple table
  6. 将数据模型减少为一个简单的表
  7. 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.
  8. 使用实体框架Power Tools Beta 2反向设计代码优先模型。这产生了一组新的错误。我知道为什么是beta。
  9. changing the ADO.NET Data Model Code Generation Strategy from None to Default to create a data model based on ObjectContext rather than DbContext
  10. 改变了ADO。NET数据模型代码生成策略从None到Default,以创建基于ObjectContext而不是DbContext的数据模型
  11. Turning off pluralisation so the property name is Mno instead of Mnoes
  12. 关闭多元化,因此属性名是Mno而不是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.

但没有一种奏效。惟一有用的是先编写代码,然后再编写dbcontext派生类和手工编写POCO。巧合的是,我发现的每个演示MVC4搭建的示例都使用这种数据模型。是否有什么地方说代码首先是与MVC4脚手架一起工作的唯一类型的数据模型?是否有人在Visual Studio 2012中率先构建了一个数据库(.edmx)数据模型?数据库非常复杂,我宁愿使用数据库优先策略。

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?

我可以看到,代码优先框架和数据库优先模型之间必须存在一些差异。例如,前者具有POCO属性,该属性持有KeyAttribute属性所指示的键,而后者持有edmx模型文件中的信息。这是实体框架工具中的逆向工程特性的基本原理吗?为了使用MVC4脚手架,我们是否期望从edmx文件转向反向工程代码优先模型?如果是这样,我们是否期望在实体框架工具完成之前继续使用动态数据项目?

4 个解决方案

#1


13  

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 ;-)

诀窍是首先编译解决方案,然后手工输入context类。不要从下拉列表中选择它,只需自己输入类名,它就会神奇地工作;

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

在这里看到的:ASP。NET MVC4—如何在MVC控制器中首先使用数据库EF

#2


0  

I don't think we can scaffold from edmx files with Entity Framework 5.

我认为我们不能用实体框架5来搭建edmx文件。

I've tried a similar set of things and am still hitting errors.

我也尝试过类似的东西,但还是会出错。

There are a couple of suggestions that maybe just deleting a bad edmx and starting again could generate the correct .tt files to allow the scaffolding to work correctly, but I've not yet seen this.

这里有一些建议,可能仅仅删除一个糟糕的edmx并重新启动就可以生成正确的.tt文件,以允许脚手架正确工作,但是我还没有看到这一点。

I'm going to try some more to get it working, as I prefer DB first approaches. I will update this answer if I get any further.

我将尝试更多来让它工作,因为我更喜欢DB first方法。如果我再进一步,我会更新这个答案。

#3


0  

The edmx code generator in Entity Frameork 5/VS 2012 is buggy. It creates code that is full of compiler errors. I'm guessing it wasn't ready for shipping when vs2012 was released, which is why they set the default code generation flag to none.

在Entity frameork5 /VS 2012中,edmx代码生成器是错误的。它创建的代码充满了编译错误。当vs2012发布时,我猜测它还没有准备好,这就是为什么他们将默认的代码生成标志设置为none。

The only question for Microsoft is: when can we expect an update that fixes this?

对微软来说,唯一的问题是:我们什么时候能看到更新来修复这个问题?

#4


-1  

It's truth! 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 ;-)

这是真理!诀窍是首先编译解决方案,然后手工输入context类。不要从下拉列表中选择它,只需自己输入类名,它就会神奇地工作;

thank!! Marcus!

谢谢! !马库斯!

#1


13  

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 ;-)

诀窍是首先编译解决方案,然后手工输入context类。不要从下拉列表中选择它,只需自己输入类名,它就会神奇地工作;

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

在这里看到的:ASP。NET MVC4—如何在MVC控制器中首先使用数据库EF

#2


0  

I don't think we can scaffold from edmx files with Entity Framework 5.

我认为我们不能用实体框架5来搭建edmx文件。

I've tried a similar set of things and am still hitting errors.

我也尝试过类似的东西,但还是会出错。

There are a couple of suggestions that maybe just deleting a bad edmx and starting again could generate the correct .tt files to allow the scaffolding to work correctly, but I've not yet seen this.

这里有一些建议,可能仅仅删除一个糟糕的edmx并重新启动就可以生成正确的.tt文件,以允许脚手架正确工作,但是我还没有看到这一点。

I'm going to try some more to get it working, as I prefer DB first approaches. I will update this answer if I get any further.

我将尝试更多来让它工作,因为我更喜欢DB first方法。如果我再进一步,我会更新这个答案。

#3


0  

The edmx code generator in Entity Frameork 5/VS 2012 is buggy. It creates code that is full of compiler errors. I'm guessing it wasn't ready for shipping when vs2012 was released, which is why they set the default code generation flag to none.

在Entity frameork5 /VS 2012中,edmx代码生成器是错误的。它创建的代码充满了编译错误。当vs2012发布时,我猜测它还没有准备好,这就是为什么他们将默认的代码生成标志设置为none。

The only question for Microsoft is: when can we expect an update that fixes this?

对微软来说,唯一的问题是:我们什么时候能看到更新来修复这个问题?

#4


-1  

It's truth! 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 ;-)

这是真理!诀窍是首先编译解决方案,然后手工输入context类。不要从下拉列表中选择它,只需自己输入类名,它就会神奇地工作;

thank!! Marcus!

谢谢! !马库斯!