I work in visual studio 2015 mvc 5 entityframework 6 code first from existing database
I need to insert data based on existing stored procedure but i dont know How to do that
what is wrong in code below
step 1
USE [EmployeeSystem]GO/****** Object: StoredProcedure [dbo].[spAddDepartment] Script Date: 13/02/2018 12:32:38 AM ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER Procedure [dbo].[spAddDepartment]@DepartmentName nvarchar(50)as Begin Insert into Departments values(@DepartmentName,1) End
USE [EmployeeSystem]
GO
/****** Object: StoredProcedure [dbo].[spAddDepartment] Script Date: 13/02/2018 12:32:38 AM ******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
ALTER Procedure [dbo].[spAddDepartment]
@DepartmentName nvarchar(50)
as
Begin
Insert into Departments values(@DepartmentName,1)
End
step2
protected override void OnModelCreating(DbModelBuilder modelBuilder){modelBuilder.Entity<Department>().MapToStoredProcedures(s => s.Insert(i => i.HasName("spAddDepartment")));base.OnModelCreating(modelBuilder);
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Department>().MapToStoredProcedures
(
s => s.Insert(i => i.HasName("spAddDepartment"))
);
base.OnModelCreating(modelBuilder);
}
step3
public ActionResult Insert(Department depart){// return View();hr.Departments.Add(depart);hr.SaveChanges();return View(depart);}
public ActionResult Insert(Department depart)
// return View();
hr.Departments.Add(depart);
hr.SaveChanges();
return View(depart);