MVC 4 + Entity Framework 5 + Stored Procedures

时间:2021-07-21 02:07:52

I'm just now teaching myself MVC4 (did webform for years), and I'm frustrated - but not about MVC which is pretty good. Entity Framework is...well

我现在正在教自己MVC4(做了多年的网络形式),我很沮丧 - 但不是关于MVC这是非常好的。实体框架是......好吧

I'm using VS2010.

我正在使用VS2010。

The problem

问题

We have a real database, you know normalized with like foreign keys and stuff. But every example I find for Entity Framework is direct to the table, but we rarely have a straight table pull - out side of populating dropdowns and such. All of our frontend calls hit a Stored Proc (how old school! expletive deleted)

我们有一个真正的数据库,你知道用外键和东西标准化。但是我为实体框架找到的每个例子都直接用于表格,但是我们很少有一个直接的表格拉出来填充下拉列表等。我们所有的前端电话都打了一个存储过程(多大的学校!删除了咒骂)

I love the Model architecture of MVC where you define the attributes of the data coming from the data source - Display Name, Ranges, DataType. etc. So I definitely want to keep this.

我喜欢MVC的模型体系结构,您可以在其中定义来自数据源的数据属性 - 显示名称,范围,数据类型。所以我绝对想保留这个。

Entity Framework and MVC do not want to play well in this senario. I created my edmx file (with SPs only), did my function import for a SP, everything is good....'til now.

实体框架和MVC不希望在这个场景中发挥出色。我创建了我的edmx文件(仅限SP),我的函数导入SP,一切都很好......直到现在。

Can't Create a Controller from the edmx/designer - Enter Controller Name, pick MVC controller with read using EF, pick the model class that is the FuntionName_Result, and for context pick the ...Entities name. FAIL Unable to retrieve metadata

无法从edmx / designer创建控制器 - 输入控制器名称,选择使用EF读取的MVC控制器,选择FuntionName_Result的模型类,并选择上下文...实体名称。失败无法检索元数据

OK, so now I try EF 5.x DbContext Generator, update the file name and boom I have a model and context - awesome now I can do the cool MVC stuff, lets rebuild the site....oh the horror - everything has been previously defined.

好的,所以现在我尝试EF 5.x DbContext Generator,更新文件名和繁荣我有一个模型和上下文 - 真棒我现在可以做酷MVC的东西,让重建网站....哦恐怖 - 一切都有之前已定义过。

I tried generating the edmx in a different folder and/or deleting it after the DBContext generator, still can't create Controller.

我尝试在不同的文件夹中生成edmx和/或在DBContext生成器之后删除它,仍然无法创建Controller。

'blah' is not part of the specified 'Context' class, and the 'Context' class could not be modifed to add a 'DbSet' property to it. (For example, the 'Context' class might be in a compiled assembly.)

IF I manually add the DBSet, I'm back to unable to retreive metadata - I am assuming this is happening because it can't connect to the DB. I don't know where to tell it to use the connection string in web.config. - if this is the problem

如果我手动添加DBSet,我又回到无法检索元数据 - 我假设这种情况正在发生,因为它无法连接到数据库。我不知道在哪里告诉它使用web.config中的连接字符串。 - 如果这是问题所在

Here lies dead my MVC hopes of a brighter future.

在这里,我的MVC希望有一个更光明的未来。

What am I missing?

我错过了什么?

I am not married to EF, so if there is a better way to access databases (without writing all the code from scratch) I'm here to listen.

我没有和EF结婚,所以如果有更好的方式来访问数据库(没有从头开始编写所有代码),我就在这里听。

Thanks

谢谢

3 个解决方案

#1


1  

Entity framework relies heavily on conventions. It takes a little bit to get used to. For example for connection strings... If entity framework doesn't find a connection string with the same name as your DBcontext class it just makes one (i think it defaults to using the project name as the database name). If this database doesn't exist it will create it locally as a sql express DB. This leads to the kinds of errors like the ones you are reporting.

实体框架在很大程度上依赖于约定。需要一点时间来习惯。例如,对于连接字符串...如果实体框架没有找到与您的DBcontext类同名的连接字符串,则它只创建一个(我认为它默认使用项目名称作为数据库名称)。如果此数据库不存在,它将在本地创建它作为sql express DB。这会导致您报告的错误类型。

If you want to define the connection string for entity framework all you need to do is provide a connection string in the web.config. Again conventions.... the connection string should be named the same as your DBContext class and entity framework will just find it.

如果要为实体框架定义连接字符串,您只需在web.config中提供连接字符串即可。再次约定....连接字符串应该与您的DBContext类相同,实体框架将找到它。

<connectionStrings>
      <add name="MyDbContextClassName" connectionString="..." />
</connectionStrings>

On an architecture note, IMHO ORM's are defiantly the way to go for new application development. It makes getting data into and out of your database soo much easier. That said it is a Big paradigm shift if you are used to accessing everything via sprocs and direct queries to the DB. Don't give up on it. It will frustrate you at first just like picking up any new tech, but it's well worth it in the end.

在体系结构说明中,恕我直言ORM是新应用程序开发的方式。它使得数据进出数据库变得更加容易。如果您习惯通过sprocs访问所有内容并直接查询数据库,那么这就是一个大范式转变。不要放弃它。一开始它会让你感到沮丧,就像拿起任何新技术一样,但最终它是值得的。

I have used entity framework and nHibernate for ORM in the past. The thing that I like about entity framework is that if you use code first migration most of the really annoying, tedious, and error prone column mappings are auto generated for you (again using conventions). You sometimes still end up having to do a little mapping but those cases are pretty rare. Maybe a little less rare if you already have a database since your column names will probably not always match entity frameworks conventions. Anyway... This is a big plus in my book and why I would defiantly favor EF over nHibernate.

我过去曾使用实体框架和nHibernate for ORM。我喜欢实体框架的是,如果你使用代码首次迁移,那么大多数真正烦人,繁琐且容易出错的列映射都是为你自动生成的(再次使用约定)。你有时候仍然需要进行一些映射,但这些情况非常罕见。如果您已经拥有数据库,可能会少一些,因为您的列名可能并不总是与实体框架约定相匹配。无论如何......这在我的书中是一个很大的优点,为什么我会偏爱EF而不是nHibernate。

#2


0  

If you already have a database, with stored procedures you do not need the EF 5.X DbContext Generator. Create either a folder in your project or a new project in your solution for your data access. In that folder/project add a edmx file and configure it to an existing database using the wizard. At this stage you can pull in your stored procedures.

如果您已有数据库,则使用存储过程不需要EF 5.X DbContext Generator。在项目中创建文件夹或在解决方案中创建新项目以进行数据访问。在该文件夹/项目中添加edmx文件,并使用向导将其配置为现有数据库。在此阶段,您可以拉入存储过程。

If you open the edmx file, you can go to the model explorer tab and manage the imported functions (stored procs) and their return types.

如果打开edmx文件,则可以转到模型资源管理器选项卡并管理导入的函数(存储过程)及其返回类型。

Once you have that, In your controller, rather than using an instance of DbContext you can just use an instance of your EF Entities. So if you called your edmx 'MyDbAccess' you should be able to use MyDbAccessEntities which will then allow you to access the stored procedures.

完成后,在控制器中,您可以只使用EF实体的实例,而不是使用DbContext的实例。因此,如果您调用edmx“MyDbAccess”,您应该能够使用MyDbAccessEntities,这将允许您访问存储过程。

#3


0  

I in your App.config file check your

我在你的App.config文件中检查你的

<connectionStrings>

After check if your stored procedures are added in the model context file

检查您的存储过程是否已添加到模型上下文文件中

YouDBModel.Context.tt
   > YourBDModel.Context.cs

#1


1  

Entity framework relies heavily on conventions. It takes a little bit to get used to. For example for connection strings... If entity framework doesn't find a connection string with the same name as your DBcontext class it just makes one (i think it defaults to using the project name as the database name). If this database doesn't exist it will create it locally as a sql express DB. This leads to the kinds of errors like the ones you are reporting.

实体框架在很大程度上依赖于约定。需要一点时间来习惯。例如,对于连接字符串...如果实体框架没有找到与您的DBcontext类同名的连接字符串,则它只创建一个(我认为它默认使用项目名称作为数据库名称)。如果此数据库不存在,它将在本地创建它作为sql express DB。这会导致您报告的错误类型。

If you want to define the connection string for entity framework all you need to do is provide a connection string in the web.config. Again conventions.... the connection string should be named the same as your DBContext class and entity framework will just find it.

如果要为实体框架定义连接字符串,您只需在web.config中提供连接字符串即可。再次约定....连接字符串应该与您的DBContext类相同,实体框架将找到它。

<connectionStrings>
      <add name="MyDbContextClassName" connectionString="..." />
</connectionStrings>

On an architecture note, IMHO ORM's are defiantly the way to go for new application development. It makes getting data into and out of your database soo much easier. That said it is a Big paradigm shift if you are used to accessing everything via sprocs and direct queries to the DB. Don't give up on it. It will frustrate you at first just like picking up any new tech, but it's well worth it in the end.

在体系结构说明中,恕我直言ORM是新应用程序开发的方式。它使得数据进出数据库变得更加容易。如果您习惯通过sprocs访问所有内容并直接查询数据库,那么这就是一个大范式转变。不要放弃它。一开始它会让你感到沮丧,就像拿起任何新技术一样,但最终它是值得的。

I have used entity framework and nHibernate for ORM in the past. The thing that I like about entity framework is that if you use code first migration most of the really annoying, tedious, and error prone column mappings are auto generated for you (again using conventions). You sometimes still end up having to do a little mapping but those cases are pretty rare. Maybe a little less rare if you already have a database since your column names will probably not always match entity frameworks conventions. Anyway... This is a big plus in my book and why I would defiantly favor EF over nHibernate.

我过去曾使用实体框架和nHibernate for ORM。我喜欢实体框架的是,如果你使用代码首次迁移,那么大多数真正烦人,繁琐且容易出错的列映射都是为你自动生成的(再次使用约定)。你有时候仍然需要进行一些映射,但这些情况非常罕见。如果您已经拥有数据库,可能会少一些,因为您的列名可能并不总是与实体框架约定相匹配。无论如何......这在我的书中是一个很大的优点,为什么我会偏爱EF而不是nHibernate。

#2


0  

If you already have a database, with stored procedures you do not need the EF 5.X DbContext Generator. Create either a folder in your project or a new project in your solution for your data access. In that folder/project add a edmx file and configure it to an existing database using the wizard. At this stage you can pull in your stored procedures.

如果您已有数据库,则使用存储过程不需要EF 5.X DbContext Generator。在项目中创建文件夹或在解决方案中创建新项目以进行数据访问。在该文件夹/项目中添加edmx文件,并使用向导将其配置为现有数据库。在此阶段,您可以拉入存储过程。

If you open the edmx file, you can go to the model explorer tab and manage the imported functions (stored procs) and their return types.

如果打开edmx文件,则可以转到模型资源管理器选项卡并管理导入的函数(存储过程)及其返回类型。

Once you have that, In your controller, rather than using an instance of DbContext you can just use an instance of your EF Entities. So if you called your edmx 'MyDbAccess' you should be able to use MyDbAccessEntities which will then allow you to access the stored procedures.

完成后,在控制器中,您可以只使用EF实体的实例,而不是使用DbContext的实例。因此,如果您调用edmx“MyDbAccess”,您应该能够使用MyDbAccessEntities,这将允许您访问存储过程。

#3


0  

I in your App.config file check your

我在你的App.config文件中检查你的

<connectionStrings>

After check if your stored procedures are added in the model context file

检查您的存储过程是否已添加到模型上下文文件中

YouDBModel.Context.tt
   > YourBDModel.Context.cs