实体框架MVC控制器 [英] Entity Framework MVC Controller

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

问题描述

我使用Visual Studio 2012年我创建了一个类项目,并加入EF数据模型这个项目。我创建了从现有的SQL Server数据库的数据模型。我创建了一个MVC 4项目,增加实体连接字符串的web配置,并增加数据项目引用。

所有这些都是好的了这一点。

现在我想添加使用实体框架的MVC控制器。我选择模型类的之一。我选择数据上下文类。单击添加。

VS弹出一个错误时,错误消息的最后一部分是类可能在编译的程序集{这是事实}


  

'AA'不是指定的aanamespace.aaEntity'阶级的一部分,实体类不能被修改,以DbSet财产吧。例如,aaEntity可能会在编译的程序集



解决方案

我想花时间来回答这个问题我自己,让其他人有类似的问题可以在将来有所帮助。

下面是我做的。

创建的.edmx文件在我的模式我的MVC项目的文件夹中。
内置的MVC项目

现在我可以为任何在.edmx文件中的表添加一个控制器。

请注意:
我认为最好的做法是创建我的解决方案中的一个项目,该项目持有.edmx文件。我将这个项目称为myData的。

我在MVC项目加入到这个项目中的引用,并尝试这种脚手架没有运气。


我希望这可以帮助别人,因为该解决方案是如此简单。

格里

I am using Visual Studio 2012. I created a Class project and added EF data model to this project. I created the data model from an existing SQL Server Database. I created a MVC 4 project, added entity connection string to web config, and added data project reference.

All is good to this point.

Now I want to add a MVC controller using Entity Framework. I select a one of the model classes. I select the data context class. Click Add.

VS pops an error up, the last part of the error message is class might be in a compiled assembly {which is true}.

'AA' is not part of the specified 'aanamespace.aaEntity' class and the entity class could not be modified to a DbSet property to it. For example the 'aaEntity' might be in a compiled assembly

解决方案

I want to take the time to answer this question myself so that others with a similar problem can be helped in the future.

Here is what I did.

Created the .edmx file in my Models folder of my MVC project. Built the MVC project

Now I can add a controller for any of the tables in the .edmx file.

Note: I thought the best practice was to create a project within my solution that holds the .edmx file. I called this project myData.

I added a reference to this project in my MVC project and tried this scaffolding without luck.


I hope this helps others because the solution is so simple.

Gerry

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

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