使用实体框架的存储过程 [英] using stored procedure in entity framework

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

问题描述

我使用asp.net的MVC 5和C#与实体框架...我有模型和领域类的功能...现在我需要使用存储过程....这我在运动中挣扎。

我下面code第一现有的数据库,我已经写存储过程存在。我的问题是我怎么能叫我的Web应用程序,存储过程。

存储过程:

  ALTER PROCEDURE [DBO]。[GetFunctionByID](
    @FunctionId INT


开始
    选择 *
    从函数的乐趣
    WHERE Function_ID = @FunctionId
结束

域类:

 公共类功能
 {
    公共功能()
    {
    }    公众诠释Function_ID {搞定;组; }
    公共字符串名称{搞定;组; }
    公众诠释Hierarchy_level {搞定;组; }
}

功能模型:

  [表(功能)
公共类App_Functions
{
    公共App_Functions()
    {
    }    [键]
    公众诠释Function_ID {搞定;组; }    [StringLength(50)]
    [需要]
    公共字符串名称{搞定;组; }    公众诠释Hierarchy_level {搞定;组; }
    //公共虚拟的ICollection< App_Controllers> App_Controllers {搞定;组; } * /
}

BaseContext:

 公共类BaseContext< TContext> :哪里的DbContext TContext:的DbContext
{
    静态BaseContext()
    {
        Database.SetInitializer< TContext>(NULL);
    }    保护BaseContext()
        :基地(NAME = ApplicationDbConnection)
    {}
}

功能方面:

 公共类FunctionsContext:BaseContext< FunctionsContext>
{
    公共DbSet< App_Functions>功能{搞定;组; }
}


解决方案

您需要创建一个包含类似下面的所有存储过程的属性模型类。
也正因为实体框架模型类需要主键,你可以通过使用GUID的假钥匙。

 公共类GetFunctionByID
{
    [键]
    公众的Guid? GetFunctionByID {搞定;组; }    //所有的其他属性。
}

然后注册 GetFunctionByID 模型类在的DbContext

 公共类FunctionsContext:BaseContext< FunctionsContext>
{
    公共DbSet< App_Functions>功能{搞定;组; }
    公共DbSet< GetFunctionByID> GetFunctionByIds {获取;设置;}
}

当你打电话给你的存储过程,只看到如下:

  VAR functionId = yourIdParameter;
变种结果= db.Database.SqlQuery&所述; GetFunctionByID&GT(GetFunctionByID @FunctionId,新的SqlParameter(@ FunctionId,functionId))了ToList())。

I am using asp.net mvc 5 and C# with Entity Framework... I have model and domain classes for function... now I need to use stored procedure.... which I am struggling at the movement.

I am following code first existing database and I have stored procedure written there. My question is how I can call that stored procedure in my web application.

Stored procedure:

ALTER PROCEDURE [dbo].[GetFunctionByID](
    @FunctionId INT
)
AS
BEGIN
    SELECT * 
    FROM Functions As Fun
    WHERE Function_ID = @FunctionId
END

Domain class:

 public class Functions
 {
    public Functions()
    {
    }

    public int Function_ID { get; set; }
    public string Title { get; set; }
    public int Hierarchy_level { get; set; }
}

Function model:

[Table("Functions")]
public class App_Functions
{
    public App_Functions()
    {
    }

    [Key]
    public int Function_ID { get; set; }

    [StringLength(50)]
    [Required]
    public string Title { get; set; }

    public int Hierarchy_level { get; set; }
    //public virtual ICollection<App_Controllers> App_Controllers { get; set; }*/
}

BaseContext:

public class BaseContext<TContext> : DbContext where TContext : DbContext
{
    static BaseContext()
    {
        Database.SetInitializer<TContext>(null);
    }

    protected BaseContext()
        : base("name = ApplicationDbConnection")
    { }
}

Function context:

public class FunctionsContext : BaseContext<FunctionsContext>
{
    public DbSet<App_Functions> Functions { get; set; }
}

解决方案

You need to create a model class that contains all stored procedure properties like below. Also because Entity Framework model class needs primary key, you can create a fake key by using Guid.

public class GetFunctionByID
{
    [Key]
    public Guid? GetFunctionByID { get; set; }

    // All the other properties.
}

then register the GetFunctionByID model class in your DbContext.

public class FunctionsContext : BaseContext<FunctionsContext>
{
    public DbSet<App_Functions> Functions { get; set; }
    public DbSet<GetFunctionByID> GetFunctionByIds {get;set;}
}

When you call your stored procedure, just see below:

var functionId = yourIdParameter;
var result =  db.Database.SqlQuery<GetFunctionByID>("GetFunctionByID @FunctionId", new SqlParameter("@FunctionId", functionId)).ToList());

这篇关于使用实体框架的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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