如何在Entity Framework中定义一个存储过程(第一个代码)? [英] How can I define a stored procedure in Entity Framework (code first)?

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

问题描述

我在Entity Framework中定义了我的模型(代码第一),但是如何在我的模型中定义一个存储过程?我想创建我的模型,并在我的数据库从我的模型生成时生成我的数据库中的存储过程。

I define my model in Entity Framework (code first), but how can I define a stored procedure in my model? I want to create my model and have it generate the stored procedure in my database when my database is generated from my model.

推荐答案

描述



目前没有为Entity Framework代码中的存储过程直接映射支持。但是,您可以像我的示例一样执行任何sql查询(创建您的存储过程)。

Description

There is no built in direct mapping support for stored procedures in Entity Framework code first at the moment. But you can exec any sql query (create your stored procedure) like in my sample.

public class MyDatabaseContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        DbCommand cmd = Database.Connection.CreateCommand();
        cmd.CommandText = "create stored procedure ....";
        cmd.ExecuteNonQuery();
    }   
}



更多信息




  • MSDN - DbCommand Class

  • More Information

    • MSDN - DbCommand Class
    • 这篇关于如何在Entity Framework中定义一个存储过程(第一个代码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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