MVC 5,EF 6和“来自数据库的第一代码”存储程序 [英] MVC 5, EF 6, and "Code First from Database" Stored Procedures

查看:125
本文介绍了MVC 5,EF 6和“来自数据库的第一代码”存储程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找答案时,即使使用引号中的数据库代码首先的关键字,我发现答案没有涉及具体的术语,所以我提出这个问题,特别强调数据库。



在使用Entity Framework 6的ASP.NET MVC 5 Web应用程序中,我希望能够使用ADO.NET实体数据模型向导映射到一个数据库上现有的存储过程。



为什么我问这个问题,我希望完成什么?



这个特定的存储过程是一个参数化的查询,用户(在View中)将通过表单形式提交日期范围,并在表中接收该查询的结果。在SQL Server端,很容易:声明两个日期并获取查询结果。



我希望用户从查看中简单地选择一个日期范围,并查看由存储过程查询提供的下面的结果。 >

以下是我尝试使用向导映射存储过程的屏幕截图。



1。指定数据库上下文的名称





2。指定首先从数据库开始





3。选择数据库连接





4。没有存储过程可用



我还注意到导入存储过程的选项是灰色的。



解决方案

编辑:Tarun提到的存储过程isn' t由于缺少权限而显示。



实体框架提供API来调用存储过程,甚至可以将存储过程映射到模型。



查看此链接:
https://msdn.microsoft.com/en-us/data/dn468673.aspx



将模型绑定到存储的程序:

  modelBuilder.Entity< Blog>()。MapToStoredProcedures(); 

如果您对映射存储过程不感兴趣,请参阅此帖:



如何在实体框架6(代码优先)中调用存储过程?


In searching for an answer, even with the keywords "Code First from Database" in quotes, I found that the answers did not address the specific terms, so I'm asking the question with a specific emphasis on "Code First from Database" only.

In an ASP.NET MVC 5 web application using Entity Framework 6, I would like to be able to use the "ADO.NET Entity Data Model" wizard to map to an existing stored procedure on a database.

Why am I asking the question and what do I hope to accomplish?

This particular stored procedure is a parameterized query, where the user (in the View) would submit by form a date range and receive the results of that query in the table. On the SQL Server side, it is easy: declare two dates and get your query results.

I would like the user, from the View, to "simply" select a date range and see the results immediately below as supplied by the stored procedure query.

Below are screenshots showing my attempt to use the wizard to map the stored procedure.

1. Specify name of database context

2. Specify "Code First From Database"

3. Choose Database Connection

4. No stored procedures are available

I also notice that the option to import stored procedures is greyed out.

解决方案

EDIT: Tarun mentioned the stored procedures aren't showing up due to lack of permissions.

Entity Framework provide APIs to call stored procedures though, you can even map a stored procedure to a model.

See this link: https://msdn.microsoft.com/en-us/data/dn468673.aspx

To bind a model to a stored procedure:

 modelBuilder.Entity<Blog>().MapToStoredProcedures();

If you are not interested in mapping a stored procedure see this post:

How to call Stored Procedure in Entity Framework 6 (Code-First)?

这篇关于MVC 5,EF 6和“来自数据库的第一代码”存储程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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