我的应用程序首先使用EF 6代码
我有一个带有一些非必需属性的模型,我使用modelbuilder来生成存储过程,问题是我不想在插入或更新期间将非必需字段发送到数据库,因为大多数都是默认约束
我试图在sqlserver中编辑存储过程的定义,但我似乎无法将其与EF同步,它说"指定了太多的参数" ,
我尝试生成迁移,然后编辑了存储过程的定义,但是当我更新数据库时,它说对象已经存在。
有没有办法将任意数量的参数发送到数据库和
有没有办法将编辑后的存储过程同步到EF
每一个建议都将受到高度赞赏
编辑: 模特
public class BattalionDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public short ID { get; set; }
[MaxLength(300)]
[Required]
[DisplayName("Battallion Name")]
public string BattalionName { get; set; }
[MaxLength(50)]
[Required]
[DisplayName("Battallion NO")]
public string BattalionNO { get; set; }
[DisplayName("Number Of Person In Batallion")]
public short NumberOfCadetsInBatallion { get; set; }
[DisplayName("Description (if any)")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public bool IsSynced { get; set; } = false;
public bool IsActive { get; set; } = true;
public bool IsDeleted { get; set; } = false;
}
这是存储过程
ALTER PROCEDURE [dbo].[BattalionDetails_Insert]
@BattalionName [nvarchar](300),
@BattalionNO [nvarchar](50),
@NumberOfCadetsInBatallion [smallint],
@Description [nvarchar](max)
AS
BEGIN
INSERT [dbo].[BattalionDetails]([BattalionName], [BattalionNO], [NumberOfCadetsInBatallion], [Description])
VALUES (@BattalionName, @BattalionNO, @NumberOfCadetsInBatallion, @Description)
DECLARE @ID smallint
SELECT @ID = [ID]
FROM [dbo].[BattalionDetails]
WHERE @@ROWCOUNT > 0 AND [ID] = scope_identity()
SELECT t0.[ID]
FROM [dbo].[BattalionDetails] AS t0
WHERE @@ROWCOUNT > 0 AND t0.[ID] = @ID
END
这是控制器部分:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "BattalionName,BattalionNO,NumberOfCadetsInBatallion,Description")] BattalionDetails battalionDetails)
{
if (ModelState.IsValid)
{
db.BattalionDetails.Add(battalionDetails);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(battalionDetails);
}
编辑2:
这是表格定义
CREATE TABLE [dbo].[BattalionDetails](
[ID] [smallint] IDENTITY(1,1) NOT NULL,
[BattalionName] [nvarchar](300) NOT NULL,
[BattalionNO] [nvarchar](50) NOT NULL,
[NumberOfCadetsInBatallion] [smallint] NOT NULL,
[Description] [nvarchar](max) NULL,
[IsSynced] [bit] NULL DEFAULT ((0)),
[IsActive] [bit] NULL DEFAULT ((1)),
[IsDeleted] [bit] NULL DEFAULT ((0)),
CONSTRAINT [PK_dbo.BattalionDetails] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
我得到的例外是
过程或函数BattalionDetails_Insert指定了太多参数
答案 0 :(得分:0)
尝试在存储过程中设置默认值
ALTER PROCEDURE [dbo].[BattalionDetails_Insert]
@BattalionName [nvarchar](300) = null,
@BattalionNO [nvarchar](50) = null,
@NumberOfCadetsInBatallion [smallint] = 0,
@Description [nvarchar](max)
AS
上面的代码设置了3个参数的默认值,现在这些参数在代码中应该是可选的。