找到你要的答案

Q:Calling Stored procedure EF6 & MVC from Inteface in the repository pattern EXAMPLE

Q:在库模式的实例调用存储过程的EF6与MVC的接口

I am using MVC 5 and Entity Framework 6 Database First approach.

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.

So, my get all or even get by ID involves this:

I have from EF a [GET/SET class] for employee A context class that has the stored procedure

I need help with IRepository.cs and Repository.cs calling the Context.pcdSELECTEMPLOYEE or Context.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);
        }

我使用MVC 5和实体框架6数据库的方法。

我找不到调用接口中存储过程的例子,返回类型是另一个问题,例如:

我的选择如下

Create Procedure pcdSELECTEMPLOYEE

@Del bit

AS

SELECT * FROM EMPLOYEE WHERE @Del = EMPLOYEE.DEL

GO

del是一个用于在数据库中删除的标志字段,用于最佳实践。

所以,我得到所有或甚至通过ID涉及到这个:

I have from EF a [GET/SET class] for employee A context class that has the stored procedure

我需要帮助irepository.cs和repository.cs调用context.pcdselectemployee或context.pcdselectemployeebyid

什么物体做我在返回类型或任何中间相?

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

请帮助这将不胜感激

谢谢

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);
        }
answer1: 回答1:

If you are using the repository pattern, then you'll have a UnitOfWork class. The calls to your stored procedures should be placed in the UnitOfWork class.

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. The stored procedure return a collection of 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. In that case, Entity Framework would create a Complex Type for you.

如果你是使用库模式,那么你就会拥有一个UnitOfWork类。调用存储过程应置于UnitOfWork类。

对于返回类型,当您在实体框架中创建函数导入时,您可以为您的存储过程返回的集合中的每个项目选择“雇员类”作为返回类型。存储过程返回雇员集合。

如果存储过程仅从雇员表中返回列的子集,则无法选择雇员类。在这种情况下,实体框架将为您创建一个复杂类型。

answer2: 回答2:

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(); } You also can use attributes on Entities not to hard code :)

  • I use 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

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(); } You also can use attributes on Entities not to hard code :)

  • I use 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
c#  asp.net-mvc  entity-framework  stored-procedures  repository-pattern