【问题标题】:implement stored procedure in code first existing database在代码优先现有数据库中实现存储过程
【发布时间】:2014-01-23 21:28:29
【问题描述】:

我正在使用 ASP.NET、C#、MVC 5 和实体框架进行项目。我已经实现了通用 DBContext 和通用存储库,并将 UnitOfWorks 用于独立的业务功能,一切正常。我需要使用定义行为的存储过程(已经与数据库一起保存),我正在努力解决这个问题

我的存储过程

USE [DORIS_DB_01]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetFunctionByID](
@FunctionId INT
)
AS
BEGIN
SELECT * 
FROM Functions As Fun
WHERE Function_ID = @FunctionId
END

如果这是正确的方法,我的意图是在 UnitOfWork 中调用存储过程

通用存储库

public interface IGenericRepository<TEntity> : IDisposable where TEntity : class
{

    IQueryable<TEntity> GetByIdAsync(int id);
    IQueryable<TEntity> GetAll();

}

存储库实现

 public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    protected DbSet<TEntity> _DbSet;
    private readonly DbContext _dbContext;

    public GenericRepository()
    {

    }

    public GenericRepository(DbContext dbContext)
    {
        this._dbContext = dbContext;
        _DbSet = _dbContext.Set<TEntity>();
    }


    public IQueryable<TEntity> GetAll()
    {
        return _DbSet;
    }

    public IQueryable<TEntity> GetByIdAsync(int id)
    {
        var receive_ID = id;

        return _DbSet;
    }

    public void Dispose()
    {

    }
}

其余代码可以在这里找到:

best practice to implement repository pattern and unitOfWork in ASP.NET MVC

【问题讨论】:

    标签: c# asp.net-mvc stored-procedures repository-pattern unit-of-work


    【解决方案1】:

    我不知道我是否理解你,但请查看http://www.codeproject.com/Questions/677043/Execute-Raw-sql-query-in-entity-framework

    本文展示了如何执行原始 sql 查询。现在在存储库中,您可以创建方法“ExecuteStoredProcedure”以将其公开给工作单元。

    【讨论】:

    • 我知道我可以使用 linq 但需要使用存储过程
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    相关资源
    最近更新 更多