filmov
tv
Part 9 Overriding stored procedure defaults with entity framework code first approach
Показать описание
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.
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.
Комментарии