【发布时间】:2014-08-06 07:42:20
【问题描述】:
这把我逼疯了。
我有一个包含几列和一个主键(bigint,非空)的表
我让 VS 创建了它的模型
public long ParcelRangeID { get; set; }
..
..
然后我创建控制器和视图。当我使用 Create View 创建新记录时,在 db.SaveChanges 上麻烦开始了:(我已将该字段包含在我的视图中为 HiddenFor)
- 没有进一步的变化;我得到一个必需的 ID 丢失错误。 (同样当我使用 DataAnnotations
[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; }
}
编辑 (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)
我正在使用 Entity Framework 5.0,将尝试更新到更高版本。
编辑 (5) 更新到 EF 6.1,同样的问题 这是我的连接字符串:
metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=XXX.XXX.XXX.XXX\DEV;initial catalog=DPDCDB_ODS;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient
【问题讨论】:
-
你能发布你的控制器代码吗?还是您调用 db.SaveChanges() 的部分?
-
将您的整个模型发布到 ParcelRange。
-
您可以发布您的查看代码吗?感觉模型绑定没做好。
-
StoreGeneratedPattern :身份。从我尝试过多次的数据库更新。
标签: c# sql asp.net-mvc entity-framework ef-database-first