Stored Procedures and EF Code First

前端 未结 4 1923
谎友^
谎友^ 2021-02-14 02:23

I would like to use a stored procedure to retrieve entities from a DB, I don\'t care about tracking changes. I just need all entities be resolved including related ones.

<
4条回答
  •  我在风中等你
    2021-02-14 02:39

    By Looking at database first approach, in auto generated context class it defines stored procedures as a virtual functions, Here I share a function from my project, this stored procedure returns a complex type of question.

    public virtual ObjectResult GetMyInnerQuestions(Nullable id)
            {
                var idParameter = id.HasValue ?
                    new ObjectParameter("Id", id) :
                    new ObjectParameter("Id", typeof(int));
    
                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("GetMyInnerQuestions", idParameter);
            }
    

    I used this in my code first do I can call stored procedures like functions as:

    IQueryable questions = db.GetMyInnerQuestions(id).AsQueryable();
    

    Hope this help

提交回复
热议问题