Problem
I get an error
Procedure or function spAddDepartment has too many arguments specified
How to solve it?
Details: when inserting data into table department
using stored procedure spAddDepartment
using fluent api in ASP.NET MVC 5, I get that error mentioned above.
Table Departments
:
CREATE TABLE [dbo].[Departments]
(
[DepartmentID] [int] IDENTITY(1,1) NOT NULL,
[DepartmentName] [nvarchar](50) NULL,
[IsActive] [bit] NULL
)
Stored procedure spAddDepartment
:
ALTER Procedure [dbo].[spAddDepartment]
@DepartmentName nvarchar(50)
AS
BEGIN
INSERT INTO Departments
VALUES (@DepartmentName, 1)
END
Department
model class:
public partial class Department
{
public Department()
{
Employees = new HashSet<Employee>();
}
public int DepartmentID { get; set; }
[StringLength(50)]
public string DepartmentName { get; set; }
public bool? IsActive { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
Database context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Department>()
.MapToStoredProcedures(p => p.Insert(sp => sp.HasName("spAddDepartment").Parameter(pm => pm.DepartmentName, "DepartmentName")));
}
DepartmentController
:
[HttpPost]
public ActionResult Insert(Department depart)
{
depart.IsActive = true;
hr.Departments.Add(depart);
hr.SaveChanges();
return View(depart);
}
Department view:
<div class="form-horizontal">
<h4>Department</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.DepartmentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DepartmentName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DepartmentName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
When you execute SaveChanges()
, it will make this call to the database (assuming the DepartmentName
is Dep1
:
exec [dbo].[spAddDepartment] @DepartmentName=N'Dep1',@IsActive=1
That is why you are getting the error because your stored procedure only has 1 parameter but EF is looking for a stored procedure with 2 parameters.
You ask why 2 parameters? Because your class has 4 properties: one is virtual so that one is ignored, the DepartmentID
is identity column so that one will be auto generated and thus not required, the other 2 properties ( DepartmentName
and IsActive
) are required and therefore it is expecting a stored procedure with 2 parameters as shown above.
Fix
To fix the issue, add another parameter to your stored procedure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.