如何在Entity Framework 6代码优先方法中MAP选择存储过程? [英] How to MAP select stored procedure in Entity Framework 6 code-first approach?

查看:52
本文介绍了如何在Entity Framework 6代码优先方法中MAP选择存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当我们写

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<tblError>().MapToStoredProcedures();
}

然后它将在数据库中创建3个存储过程,其名称为 tblError_Delete tblError_Insert tblError_Update

Then it will create 3 stored procedures in the database with the names of tblError_Delete, tblError_Insert, tblError_Update

我的问题是关于 tblError_Select 的:如何映射用于执行选择查询的存储过程?

My question is about tblError_Select: how to map a stored procedure that is used to execute select queries?

我希望EF与前面的3个存储过程一起生成 tblError_Select .

I want EF to be generate tblError_Select along with preceding 3 stored procedures.

我想使用带有EF的存储过程进行CRUD操作.目前,我可以插入,更新,删除,但是选择呢?为什么EF不为 Select 操作创建存储过程?

I want to do CRUD operation using stored procedures with EF. Currently I can do Insert, Update, Delete but what about Select? Why does EF not create a stored procedure for Select operation?

推荐答案

由于此问题带来了一些新见解,因此我进行了一些研究,回答了marc_s很有帮助.基本上了解EF Code-First,如果您没有任何数据库,则无法为Select创建SP,因为您必须先创建数据库,然后在其中插入数据,然后选择其中的内容(这更有意义),必须在SQL中创建存储过程并使用下面的代码运行它:您可以按如下方法在 DbContext 类中调用存储过程.

Since this question brought some new insights, I've researched a little and marc_s answered helped. Basically Knowing EF Code-First, when you do not have any database it is kinda impossible to create a SP for Select, since you have to create the database first then insert data within then select what is there (which makes more sense), you have to create the Stored Procedure within the SQL and use code below to run it: You can call a stored procedure in your DbContext class as follows.

this.Database.SqlQuery<YourEntityType>("storedProcedure‌​Name",params);

贷方@Marc_S和来源

Credit goes to @Marc_S and Source

这篇关于如何在Entity Framework 6代码优先方法中MAP选择存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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