Part 9 Overriding stored procedure defaults with entity framework code first approach

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

Entity Framework - All Text Articles

Entity Framework - All Slides

Entity Framework Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

In this video we will discuss, changing the default Insert, Update and Delete stored procedure names that are auto-generated by entity framework code first approach. This is continuation to Part 8. Please watch Part 8 before proceeding.

public class EmployeeDBContext : DbContext
{
public DbSet[Employee] Employees { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity[Employee]().MapToStoredProcedures();
base.OnModelCreating(modelBuilder);
}
}

By default, the code above generates the following 3 stored procedures for Inserting, Updating and Deleting Employee objects.
Employee_Insert
Employee_Update
Employee_Delete

If you want to override or change the default names of auto-generated stored procedures, change the code in EmployeeDBContext class as shown below.
public class EmployeeDBContext : DbContext
{
public DbSet[Employee] Employees { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity[Employee]().MapToStoredProcedures(p =] p.Insert(x =] x.HasName("InsertEmployee")));
modelBuilder.Entity[Employee]().MapToStoredProcedures(p =] p.Update(x =] x.HasName("UpdateEmployee")));
modelBuilder.Entity[Employee]().MapToStoredProcedures(p =] p.Delete(x =] x.HasName("DeleteEmployee")));

base.OnModelCreating(modelBuilder);
}
}

At this point delete the Sample database and run WebForm1 again. Notice that the generated stored procedures now have the names we specified.

The above code can also be rewritten as shown below
public class EmployeeDBContext : DbContext
{
public DbSet[Employee] Employees { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity[Employee]().MapToStoredProcedures
(p =] p.Insert(i =] i.HasName("InsertEmployee"))
.Update(u =] u.HasName("UpdateEmployee"))
.Delete(d =] d.HasName("DeleteEmployee"))
);
base.OnModelCreating(modelBuilder);
}
}

The default parameter names of the stored procedures can also be changed using the following code.
public class EmployeeDBContext : DbContext
{
public DbSet[Employee] Employees { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity[Employee]().MapToStoredProcedures
(p =] p.Insert(i =] i.HasName("InsertEmployee")
.Parameter(n =] n.Name, "EmployeeName")
.Parameter(n =] n.Gender, "EmployeeGender")
.Parameter(n =] n.Salary, "EmployeeSalary"))
.Update(u =] u.HasName("UpdateEmployee")
.Parameter(n =] n.ID, "EmployeeID")
.Parameter(n =] n.Name, "EmployeeName")
.Parameter(n =] n.Gender, "EmployeeGender")
.Parameter(n =] n.Salary, "EmployeeSalary"))
.Delete(d =] d.HasName("DeleteEmployee")
.Parameter(n =] n.ID, "EmployeeID"))
);
base.OnModelCreating(modelBuilder);
}
}

At this point drop the Sample database and run WebForm1 again. Notice that the stored procedure parameters have the names we specified.
Рекомендации по теме
Комментарии
Автор

i got confused in this tutorial., i watched your previous video and i noticed you didnt relatively used the stored procedure in inserting, updating etc. instead you used repository. and basically you can achieved that without overriding the stored procedure i think., how can you override the default procedure in stored procedure and use it in inserting, updating etc,
Ex.
I have another table called department and in my Web i need to fill up employee information along with the department he belongs. if the department he belongs does not exist in the list the user will add new department and when the user save the data it will invoke the stored procedure where it will save the employee information and the department information. pls reply to this. thank you

paloy