How to call Stored Procedure in Entity Framework 6 (Code-First)?

前端 未结 21 2009
滥情空心
滥情空心 2020-11-22 05:04

I am very new to Entity Framework 6 and I want to implement stored procedures in my project. I have a stored procedure as follows:

ALTER PROCEDURE [dbo].[ins         


        
相关标签:
21条回答
  • 2020-11-22 05:44

    I found that calling of Stored Procedures in Code First approach is not convenient. I prefer to use Dapper instead

    The following code was written with Entity Framework:

    var clientIdParameter = new SqlParameter("@ClientId", 4);
    
    var result = context.Database
    .SqlQuery<ResultForCampaign>("GetResultsForCampaign @ClientId", clientIdParameter)
    .ToList();
    

    The following code was written with Dapper:

    return Database.Connection.Query<ResultForCampaign>(
                "GetResultsForCampaign ",
                new
                {
                    ClientId = 4
                },
                commandType: CommandType.StoredProcedure);
    

    I believe the second piece of code is simpler to understand.

    0 讨论(0)
  • 2020-11-22 05:45

    Mindless passenger has a project that allows for multiple results sets to be returned from a stored proc using entity framework. One of his examples below....

    using (testentities te = new testentities())
    {
        //-------------------------------------------------------------
        // Simple stored proc
        //-------------------------------------------------------------
        var parms1 = new testone() { inparm = "abcd" };
        var results1 = te.CallStoredProc<testone>(te.testoneproc, parms1);
        var r1 = results1.ToList<TestOneResultSet>();
    }
    
    0 讨论(0)
  • 2020-11-22 05:45

    You can pass parameters to sp_GetById and fetch the results either in ToList() or FirstOrDefault();

    var param  = new SqlParameter("@id", 106);
    var result = dbContext
                   .Database
                   .SqlQuery<Category>("dbo.sp_GetById @id", param)
                   .FirstOrDefault();
    
    0 讨论(0)
  • 2020-11-22 05:50

    if you wanna pass table params into stored procedure, you must necessary set TypeName property for your table params.

    SqlParameter codesParam = new SqlParameter(CODES_PARAM, SqlDbType.Structured);
                SqlParameter factoriesParam = new SqlParameter(FACTORIES_PARAM, SqlDbType.Structured);
    
                codesParam.Value = tbCodes;
                codesParam.TypeName = "[dbo].[MES_CodesType]";
                factoriesParam.Value = tbfactories;
                factoriesParam.TypeName = "[dbo].[MES_FactoriesType]";
    
    
                var list = _context.Database.SqlQuery<MESGoodsRemain>($"{SP_NAME} {CODES_PARAM}, {FACTORIES_PARAM}"
                    , new SqlParameter[] {
                       codesParam,
                       factoriesParam
                    }
                    ).ToList();
    
    0 讨论(0)
  • 2020-11-22 05:51

    You are using MapToStoredProcedures() which indicates that you are mapping your entities to stored procedures, when doing this you need to let go of the fact that there is a stored procedure and use the context as normal. Something like this (written into the browser so not tested)

    using(MyContext context = new MyContext())
    {
        Department department = new Department()
        {
            Name = txtDepartment.text.trim()
        };
        context.Set<Department>().Add(department);
    }
    

    If all you really trying to do is call a stored procedure directly then use SqlQuery

    0 讨论(0)
  • 2020-11-22 05:53
    public IList<Models.StandardRecipeDetail> GetRequisitionDetailBySearchCriteria(Guid subGroupItemId, Guid groupItemId)
    {
        var query = this.UnitOfWork.Context.Database.SqlQuery<Models.StandardRecipeDetail>("SP_GetRequisitionDetailBySearchCriteria @SubGroupItemId,@GroupItemId",
        new System.Data.SqlClient.SqlParameter("@SubGroupItemId", subGroupItemId),
        new System.Data.SqlClient.SqlParameter("@GroupItemId", groupItemId));
        return query.ToList();
    }
    
    0 讨论(0)
提交回复
热议问题