I am using asp.net mvc 5 and C# with Entity Framework... I have model and domain classes for function... now I need to use stored procedure.... which I am struggling at the movement.
I am following code first existing database and I have stored procedure written there. My question is how I can call that stored procedure in my web application.
Stored procedure:
ALTER PROCEDURE [dbo].[GetFunctionByID]( @FunctionId INT ) AS BEGIN SELECT * FROM Functions As Fun WHERE Function_ID = @FunctionId END
Domain class:
public class Functions { public Functions() { } public int Function_ID { get; set; } public string Title { get; set; } public int Hierarchy_level { get; set; } }
Function model:
[Table("Functions")] public class App_Functions { public App_Functions() { } [Key] public int Function_ID { get; set; } [StringLength(50)] [Required] public string Title { get; set; } public int Hierarchy_level { get; set; } //public virtual ICollection<App_Controllers> App_Controllers { get; set; }*/ }
BaseContext:
public class BaseContext<TContext> : DbContext where TContext : DbContext { static BaseContext() { Database.SetInitializer<TContext>(null); } protected BaseContext() : base("name = ApplicationDbConnection") { } }
Function context:
public class FunctionsContext : BaseContext<FunctionsContext> { public DbSet<App_Functions> Functions { get; set; } }
The Entity Framework has the capability of importing a Stored Procedure as a function. We can also map the result of the function back to any entity type or complex type.
Stored procedures are one of the key advantages that the Microsoft SQL server provides. For boosting the query performance, the complex query should be written at the database level through stored procedures. Microsoft . NET Core supports calling of raw query and store procedure through entity framework.
Expand the stored procedure item and select your stored procedure from the list. Now click finish and save the Model. Now open the Model Browser and expand Entity Container and right-click on "Function Imports" and click "Add Function Import…". Click on and save the model.
You need to create a model class that contains all stored procedure properties like below. Also because Entity Framework model class needs primary key, you can create a fake key by using Guid.
public class GetFunctionByID { [Key] public Guid? GetFunctionByID { get; set; } // All the other properties. }
then register the GetFunctionByID
model class in your DbContext
.
public class FunctionsContext : BaseContext<FunctionsContext> { public DbSet<App_Functions> Functions { get; set; } public DbSet<GetFunctionByID> GetFunctionByIds {get;set;} }
When you call your stored procedure, just see below:
var functionId = yourIdParameter; var result = db.Database.SqlQuery<GetFunctionByID>("GetFunctionByID @FunctionId", new SqlParameter("@FunctionId", functionId)).ToList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With