Is that possible to update multiple procedure and function definition in EDMX file C# DB first approach

62 views Asked by At

I have to update many procs and function in edmx, and I don't want update 1 by 1 in Model browser and also don't want to re-create edmx , So is there any possible solution there, i can update multiple proc or function in edmx file in one go.

1

There are 1 answers

0
Jack J Jun On

You can refer to the following to the followings to update multiple proc or function in one go.

First, please right click the edmx file and choose the update model from database.

enter image description here

Second, If you want to add the new procedures, you can click Add and choose the correspond stored procedure.

enter image description here

If you want to update the existed procedure, you can click Refresh and choose the stored procedure.

enter image description here

Third, you can see the correspond method in the dbcontext.cs file.

public virtual ObjectResult<TestProcedure_Result> TestProcedure(Nullable age) { var ageParameter = age.HasValue ? new ObjectParameter("age", age) : new ObjectParameter("age", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<TestProcedure_Result>("TestProcedure", ageParameter);
    }

    public virtual ObjectResult<Pro1_Result> Pro1(Nullable<int> age)
    {
        var ageParameter = age.HasValue ?
            new ObjectParameter("age", age) :
            new ObjectParameter("age", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Pro1_Result>("Pro1", ageParameter);
    }

    public virtual ObjectResult<string> Proc2(Nullable<int> score)
    {
        var scoreParameter = score.HasValue ?
            new ObjectParameter("score", score) :
            new ObjectParameter("score", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<string>("Proc2", scoreParameter);
}

Finally, you can use the following code to get the result by using the stored procedure.

 using (var db = new StudentContext())
            {
                var result = db.TestProcedure(20);
                foreach (var stu in result)
                {
                    Console.WriteLine(stu.Name);
                }
            }