简体   繁体   English

在存储库模式中从Inteface调用存储过程EF6&MVC

[英]Calling Stored procedure EF6 & MVC from Inteface in the repository pattern EXAMPLE

I am using MVC 5 and Entity Framework 6 Database First approach. 我正在使用MVC 5和Entity Framework 6数据库优先方法。

I cannot find an example of calling a stored procedure in the interface and the return type is another problem as for example: 我找不到在接口中调用存储过程的示例,返回类型是另一个问题,例如:

My Select is as follows 我的选择如下

Create Procedure pcdSELECTEMPLOYEE

@Del bit

AS

SELECT * FROM EMPLOYEE WHERE @Del = EMPLOYEE.DEL

GO

Del is a flag field used for deleting in the database for best practice. Del是一个标记字段,用于在数据库中进行删除以实现最佳实践。

So, my get all or even get by ID involves this: 因此,我获得全部或什至获得ID都涉及到:

I have from EF a [GET/SET class] for employee A context class that has the stored procedure 我从EF获得了一个用于员工的[GET / SET类]一个具有存储过程的上下文类

I need help with IRepository.cs and Repository.cs calling the Context.pcdSELECTEMPLOYEE or Context.pcdSELECTEMPLOYEEBYID 我需要有关IRepository.cs和Repository.cs的帮助,调用Context.pcdSELECTEMPLOYEEContext.pcdSELECTEMPLOYEEBYID

What objects do I set in the interphace for return types or anything? 我应该在相隔中为返回类型或其他内容设置哪些对象?

public interface IRepository<T>
{
    IQueryable<T> GetAll();
    // other CRUD methods here...
} 

Please help this would be much appreciated 请帮助,将不胜感激

Thanks 谢谢

Entity Framework creates the following public virtual 实体框架创建以下公共虚拟

ObjectResult<pcdSelectStaffBID_Result>pcdSelectStaffBID(Nullable<int> iD)
        {
            var iDParameter = iD.HasValue ?
                new ObjectParameter("ID", iD) :
                new ObjectParameter("ID", typeof(int));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<pcdSelectStaffBID_Result>("pcdSelectStaffBID", iDParameter);
        }

If you are using the repository pattern, then you'll have a UnitOfWork class. 如果您使用的是存储库模式,则将有一个UnitOfWork类。 The calls to your stored procedures should be placed in the UnitOfWork class. 对存储过程的调用应放在UnitOfWork类中。

As for the return type, when you create the Function Import in Entity Framework, you can select the Employee class as the return type for each item in the collection that's returned by your stored procedure. 至于返回类型,在实体框架中创建函数导入时,可以选择Employee类作为存储过程返回的集合中每个项目的返回类型。 The stored procedure return a collection of Employees. 该存储过程返回Employees的集合。

If your stored procedure returned only a subset of the columns from the Employee table, then you would not be able to choose the Employee class. 如果您的存储过程仅返回Employee表中列的子集,则您将无法选择Employee类。 In that case, Entity Framework would create a Complex Type for you. 在这种情况下,实体框架将为您创建复杂类型。

You may create you own EFRepository class which implements IRepository, and in GetAll() do like this if( T.Gettype()=typeof(Employee)) { _dbContext.SqlQuery<Employee>(sqltext, params).ToArray(); } 您可以创建自己的EFRepository类来实现IRepository,并在GetAll()中执行以下操作if( T.Gettype()=typeof(Employee)) { _dbContext.SqlQuery<Employee>(sqltext, params).ToArray(); } if( T.Gettype()=typeof(Employee)) { _dbContext.SqlQuery<Employee>(sqltext, params).ToArray(); } You also can use attributes on Entities not to hard code :) if( T.Gettype()=typeof(Employee)) { _dbContext.SqlQuery<Employee>(sqltext, params).ToArray(); }您还可以使用在实体的属性没有硬编码:)

  • I use public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate = null) { return (predicate == null) ? _context.Set<T>() : _context.Set<T>().Where(predicate); } 我使用public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate = null) { return (predicate == null) ? _context.Set<T>() : _context.Set<T>().Where(predicate); } public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate = null) { return (predicate == null) ? _context.Set<T>() : _context.Set<T>().Where(predicate); } public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate = null) { return (predicate == null) ? _context.Set<T>() : _context.Set<T>().Where(predicate); } to filter on db level public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate = null) { return (predicate == null) ? _context.Set<T>() : _context.Set<T>().Where(predicate); }要过滤的分贝水平

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM