这个快把我逼疯了。
我有一个表,有几个列和一个主键(bigint,不是null)
我让VS为它创建一个模型
public long ParcelRangeID { get; set; }
..
..
然后我创建控制器和视图。当我使用创建视图创建一个新的记录,在数据库。SaveChanges故障开始:(我已经在我的视图中包含字段为HiddenFor)
- 没有进一步的变化;我得到一个必需的ID是缺失的错误。(也当我使用数据注释
[Key]
,[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
和[HiddenInput(DisplayValue = false)]
- 当我使用
[Bind(Exclude="ParcelRangeID")]
时,我得到DbUpdateConcurrencyException。
如何插入新记录并自动生成所需的ID而不会出现错误?
编辑
控制器代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ParcelRange parcelrange)
{
if (ModelState.IsValid)
{
db.ParcelRange.Add(parcelrange);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(parcelrange);
}
和模型:
public partial class ParcelRange
{
public ParcelRange()
{
this.PickupAddressID = -1;
this.OrganizationID = -1;
this.DepotID = -1;
this.IsVTG = 0;
this.IsInternal = 0;
this.IsInterCompany = 0;
this.UpdateCount = 0;
this.IsActive = 1;
this.Modified = DateTime.Now.Date;
this.Created = DateTime.Now.Date;
this.ValidFrom = DateTime.Now.Date;
this.ValidTo = DateTime.MaxValue;
}
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Key]
[HiddenInput(DisplayValue = false)]
public long ParcelRangeID { get; set; }
public long NdswCustomerID { get; set; }
public long PickupAddressID { get; set; }
public long OrganizationID { get; set; }
public long DepotID { get; set; }
public string ParcelRangePrefix { get; set; }
public string ParcelNumberStart { get; set; }
public string ParcelNumberEnd { get; set; }
public byte IsVTG { get; set; }
public byte IsInternal { get; set; }
public byte IsInterCompany { get; set; }
public byte[] RowVer { get; set; }
public System.DateTime Created { get; set; }
public System.DateTime Modified { get; set; }
public int UpdateCount { get; set; }
public System.DateTime ValidFrom { get; set; }
public System.DateTime ValidTo { get; set; }
public byte IsActive { get; set; }
}
Edit (2)
最后是(默认)自动生成的视图
@model MvcApplication1.ParcelRange
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.ParcelRangeID)
<fieldset>
<legend>ParcelRange</legend>
<div class="editor-label">
@Html.LabelFor(model => model.NdswCustomerID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NdswCustomerID)
@Html.ValidationMessageFor(model => model.NdswCustomerID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PickupAddressID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PickupAddressID)
@Html.ValidationMessageFor(model => model.PickupAddressID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OrganizationID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OrganizationID)
@Html.ValidationMessageFor(model => model.OrganizationID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DepotID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DepotID)
@Html.ValidationMessageFor(model => model.DepotID)
</div>
@* <div class="editor-label">
@Html.LabelFor(model => model.ParcelRangePrefix)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParcelRangePrefix)
@Html.ValidationMessageFor(model => model.ParcelRangePrefix)
</div>*@
<div class="editor-label">
@Html.LabelFor(model => model.ParcelNumberStart)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParcelNumberStart)
@Html.ValidationMessageFor(model => model.ParcelNumberStart)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ParcelNumberEnd)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ParcelNumberEnd)
@Html.ValidationMessageFor(model => model.ParcelNumberEnd)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsVTG)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsVTG)
@Html.ValidationMessageFor(model => model.IsVTG)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsInternal)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsInternal)
@Html.ValidationMessageFor(model => model.IsInternal)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsInterCompany)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsInterCompany)
@Html.ValidationMessageFor(model => model.IsInterCompany)
</div>
@* <div class="editor-label">
@Html.LabelFor(model => model.Created)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Created)
@Html.ValidationMessageFor(model => model.Created)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Modified)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Modified)
@Html.ValidationMessageFor(model => model.Modified)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UpdateCount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UpdateCount)
@Html.ValidationMessageFor(model => model.UpdateCount)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ValidFrom)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ValidFrom)
@Html.ValidationMessageFor(model => model.ValidFrom)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ValidTo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ValidTo)
@Html.ValidationMessageFor(model => model.ValidTo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsActive)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive)
</div>*@
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
编辑(3)表的数据库脚本:
USE [DPDCDB_ODS]
GO
/****** Object: Table [dbo].[ParcelRange] Script Date: 16-6-2014 15:50:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ParcelRange](
[ParcelRangeID] [bigint] IDENTITY(1,1) NOT NULL,
[NdswCustomerID] [bigint] NOT NULL,
[PickupAddressID] [bigint] NOT NULL,
[OrganizationID] [bigint] NOT NULL,
[DepotID] [bigint] NOT NULL,
[ParcelRangePrefix] AS (left([ParcelNumberStart],(4))),
[ParcelNumberStart] [nvarchar](50) NOT NULL,
[ParcelNumberEnd] [nvarchar](50) NOT NULL,
[IsVTG] [tinyint] NOT NULL,
[IsInternal] [tinyint] NOT NULL,
[IsInterCompany] [tinyint] NOT NULL,
[RowVer] [timestamp] NOT NULL,
[Created] [datetime] NOT NULL,
[Modified] [datetime] NOT NULL,
[UpdateCount] [int] NOT NULL,
[ValidFrom] [date] NOT NULL,
[ValidTo] [date] NOT NULL,
[IsActive] [tinyint] NOT NULL,
CONSTRAINT [PK_ParcelRange] PRIMARY KEY CLUSTERED
(
[ParcelRangeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [DATA]
) ON [DATA]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((-1)) FOR [NdswCustomerID]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((-1)) FOR [PickupAddressID]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((-1)) FOR [OrganizationID]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((-1)) FOR [DepotID]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((0)) FOR [IsVTG]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((0)) FOR [IsInternal]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((0)) FOR [IsInterCompany]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT (getdate()) FOR [Created]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT (getdate()) FOR [Modified]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((0)) FOR [UpdateCount]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT (getdate()) FOR [ValidFrom]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT (CONVERT([date],'9999-12-31')) FOR [ValidTo]
GO
ALTER TABLE [dbo].[ParcelRange] ADD DEFAULT ((1)) FOR [IsActive]
GO
编辑(4)
我正在使用实体框架5.0,将尝试更新到更高版本。
编辑(5)更新到EF 6.1,同样的问题这是我的connectionstring:
metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=XXX.XXX.XXX.XXXDEV;initial catalog=DPDCDB_ODS;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient
"(I have included the field in my view as HiddenFor)"
因为它是一个数据库生成的主键,在create
视图中它没有初始值,你不应该使用HiddenFor
来保存该属性。把HiddenFor
删掉。
如果您使用scaffolding向导来创建EF控制器,您应该注意主键在create
视图中永远不会出现。
更新:http://robjoosentest.azurewebsites.net/parcelranges
我使用了提供的表脚本,创建了一个数据库,将其导出到Azure,创建了一个VS 2013 MVC web应用程序,然后为表创建了基于EF模型的控制器和视图。然后我把它发布到Azure上,它运行得很好(微软做得很好,让我们看看任何人在5分钟内就能在亚马逊S3上做到这一点!):)。我怀疑这是你的一个库的版本(可能是EF版本)。否则,它可能是web应用程序上其他数据库活动的副作用。
当我向我们的DBA提出这个问题时,他立即为我指出了正确的方向。
在这个特定的表.....上有触发器这些触发器会导致DBUpdateConcurrencyException。我们禁用了这些触发器,插入操作很顺利
我们现在必须找出如何模仿c#中的触发器或使用STP来更新/插入EF记录。
感谢您的支持!