C# 过程或函数SpAddDepartment指定的参数太多

C# 过程或函数SpAddDepartment指定的参数太多,c#,asp.net-mvc,entity-framework,entity-framework-6,ef-fluent-api,C#,Asp.net Mvc,Entity Framework,Entity Framework 6,Ef Fluent Api,问题 我犯了一个错误 过程或函数spAddDepartment指定的参数太多 如何解决 详细信息:在ASP.NET MVC 5中使用fluent api,使用存储过程将数据插入表department时,我遇到了上面提到的错误 表部门: CREATE TABLE [dbo].[Departments] ( [DepartmentID] [int] IDENTITY(1,1) NOT NULL, [DepartmentName] [nvarchar](50) NULL, [I

问题

我犯了一个错误

过程或函数spAddDepartment指定的参数太多

如何解决

详细信息:在ASP.NET MVC 5中使用fluent api,使用存储过程将数据插入表
department
时,我遇到了上面提到的错误

部门

CREATE TABLE [dbo].[Departments]
(
    [DepartmentID] [int] IDENTITY(1,1) NOT NULL,
    [DepartmentName] [nvarchar](50) NULL,
    [IsActive] [bit] NULL
)
存储过程spAddDepartment:

ALTER Procedure [dbo].[spAddDepartment]
    @DepartmentName    nvarchar(50)
AS
BEGIN
    INSERT INTO Departments 
    VALUES (@DepartmentName, 1) 
END
部门
型号等级:

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; }
}
部门视图:

<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>

部门

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.DepartmentName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.DepartmentName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.DepartmentName,“,new{@class=“text danger”}) @LabelFor(model=>model.IsActive,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.IsActive) @Html.ValidationMessageFor(model=>model.IsActive,“,new{@class=“text danger”})
执行
SaveChanges()
时,它将对数据库进行此调用(假设
部门名称为
Dep1

exec [dbo].[spAddDepartment] @DepartmentName=N'Dep1',@IsActive=1
这就是为什么会出现错误,因为您的存储过程只有1个参数,而EF正在查找一个有2个参数的存储过程

您会问为什么有2个参数?因为您的类有4个属性:一个是虚拟的,所以一个被忽略;
DepartmentID
是标识列,所以一个将自动生成,因此不需要;另外2个属性(
DepartmentName
是活动的
)是必需的,因此它需要一个具有2个参数的存储过程,如上所示

修复


若要解决此问题,请向存储过程中添加另一个参数。

运行时显示错误
<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>
exec [dbo].[spAddDepartment] @DepartmentName=N'Dep1',@IsActive=1