ahmed salah

ahmed salah

  • 1.6k
  • 530
  • 121.6k

How to insert data using existing stored procedure mvc

Feb 12 2018 5:38 PM

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 ON
GO
SET QUOTED_IDENTIFIER ON
GO
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);

}

step3

public ActionResult Insert(Department depart)
{
// return View();
hr.Departments.Add(depart);
hr.SaveChanges();
return View(depart);
}


Answers (4)