• About Me
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Advertise / Sponsor

DOTNET DETAIL

Learn Microsoft .NET Technologies

  • Home
  • Tutorials
    • Angular
      • Angular 5
      • Angular 6
    • ASP.NET Core
    • Azure
    • React
    • Vue
  • Books
  • Courses
  • Cloud Hosting
  • Interview Questions
You are here: Home / ASP.NET Core / Entity Framework Core & Stored Procedure using Fluent API

Entity Framework Core & Stored Procedure using Fluent API

October 2, 2019 by mebakar1005 1 Comment

In this tutorial, we are going to cover crud operations (create, read, update, and delete) using entity framework core & stored procedure with Fluent API. So, first-of-all, we will add a model class in our project and create a database using migrations and then we will create stored procedures for insert, update and delete using Fluent API. Then we will perform insert, update and delete operations using entity framework core.

Entity framework core & stored procedure using fluent API

Let’s see how to perform crud operations using entity framework core & stored procedure with Fluent API.

There are two methods in Entity Framework Core to execute a stored procedure

  • FormSql()
  • ExecuteSqlCommand()

Note: – In entity framework core, there are some limitations to execute a stored procedure using FormSql() or ExecuteSqlCommand()

  • It must return all the properties of the corresponding table of an entity.
  • It cannot contain related data. It Can’t perform joins to formulate the result.
  • Stored Procedures cannot be mapped with the entity. It means we cannot call SaveChanges method to call Insert, Update, and Delete operations.

Step # 1: Create a database

In this step, we will create a model class with the name of employee and then create a database using migrations. So, add a new class in your Models folder and then add the properties as you do see in the below code file.

Now, add DbSet<Employee> property in the DbContext class and then run these below commands in the Package Manager Console to create a database.

=> add-migration InitialCommit

=> update-database

These above commands will create a new database as you do see in the below screenshot.

See also  Define one to many relationship in Entity Framework Code First

Step # 2: How to Create stored Stored Procedures using Fluent API?

In this step, we will see how to create stored procedures for insert, update and delete using fluent API. So, go to DbContext class and then add the model builder within the OnModelCreating function as you do see below in the code file.

Note: – don’t forget to import libraries.

Now go to package manager console and then run this below command.

=> add-migration createSP

This above command will show you a partial createSP class. And this class have some code to create stored procedures for insert, update and delete as you do see in the below file code.

Now, run below command to update database.

=> update-database

Now, go to database and then you will see the output as you do see in the below screenshot.

Step # 3: How to execute stored procedure in Entity Framework Core?

Now, in this step we will see how to execute a stored procedure using entity framework core. As you know, we have three stored procedures like Employee_Insert , Employee_Update and Employee_Delete. 

Insert Record: So, we can insert the record in database using stored procedure in entity framework core by following the below code.

This above code will insert the record in database

Update Record: We can update the record in database using stored procedure in entity framework core by following the below code.

Delete Record: We can delete the record in the database using stored procedure in entity framework core by following the below code.

Thank you for reading. Please like and share within your community!

Define one to many relationship in Entity Framework Code First
How to Create an Angular 8 and Asp.Net Core 3 app using VS2019

Related

Filed Under: ASP.NET Core, Entity Framework Tagged With: Entity Framework Core, Stored Procedure

Comments

  1. Umer says

    February 6, 2020 at 3:28 pm

    this .MapToStoredProcedures(); is NOT available in Entity Framework core…. only in Entity Framework! Help please.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Buy me a coffeeBuy me a coffee

Jobs

Dotnet Jobs

Join Us

Join Us

Subscribe to Blog via Email

Enter your email address to subscribe.

Recent Posts

  • Top LINQ interview questions and answers.
  • Difference between .NET Core and .NET Framework
  • Top 10 Programming Languages of the Future.
  • Top MVC Interview Questions & Answers
  • 20 best Data Science Books: Beginner to Advanced Level
  • 10 Best Machine Learning Laptops
Copyright © 2022