C# 如何解决空DB表上的乐观并发异常错误
使用ASP.NET MVC,实体框架在SQL Server数据库上执行CRUD操作。我正在将新信息插入连接的表中。我在许多表上执行此操作,但我以前从未遇到此错误 当我将数据提交给控制器时,它会遍历典型的EFF内容。但是,当它点击我的C# 如何解决空DB表上的乐观并发异常错误,c#,asp.net-mvc,entity-framework,controller,C#,Asp.net Mvc,Entity Framework,Controller,使用ASP.NET MVC,实体框架在SQL Server数据库上执行CRUD操作。我正在将新信息插入连接的表中。我在许多表上执行此操作,但我以前从未遇到此错误 当我将数据提交给控制器时,它会遍历典型的EFF内容。但是,当它点击我的db.SaveChanges()行时,我得到了这个乐观并发异常错误,表示数据可能已从数据库中更改。在这种特殊情况下,表中没有数据,因此对我来说没有意义 我遵循了这个建议,读了一些关于它的文章。实现了他们的一个修复程序,但不起作用。无法工作,因为它正在查找GetData
db.SaveChanges()
行时,我得到了这个乐观并发异常错误,表示数据可能已从数据库中更改。在这种特殊情况下,表中没有数据,因此对我来说没有意义
我遵循了这个建议,读了一些关于它的文章。实现了他们的一个修复程序,但不起作用。无法工作,因为它正在查找GetDatabaseValues()
,但没有。我错过了一些愚蠢的事情吗?我只是将表单值插入到我设置的两个不同的表中
这是控制器:
[HttpPost]
public ActionResult SubmitUwSelections(FormCollection form)
{
var cbRating = form["cabRating"]; // Cab Raing ddl
var filing = form["fileRqd"]; // filing required ddl
var ifta = form["cbIfta"]; // IFTA cb
var lossControl = form["lcRqd"]; // Loss control cb
var app1 = form["AppInFile"]; //Application app in file cb
var app2 = form["AppRqd"]; //Application app rqd cb
var um1 = form["UMInFile"]; //UM in file cb
var um2 = form["UMRqd"]; // UM rqd cb
var terror1 = form["terrorInFile"]; // terror in file cb
var terror2 = form["terrorRqd"]; // terror rqd cb
var lossRun1 = form["LossRunInFile"];// loss run in file cb
var lossRun2 = form["LossRunRqd"]; //Loss run rqd cb
var inspect1 = form["cbVehicleInspectInFile"]; //vehicle inspect in file cb
var inspect2 = form["cbVehicleInspectRqd"]; // vehicle inspect rqd cb
var mvr1 = form["cbMvrInFile"]; // mvr in file cb
var mvr2 = form["cbMvrRqd"]; // mvr rqd cb
var psComments = form["cifComments"]; //section 2 comment box
var uniqPolicy = form["UniqPolicy"];
string c = uniqPolicy.ToString();
int u = Convert.ToInt32(c);
// need to form up and create variables for the segmentation part of the insert.
// Segmentation pieces.
var segYear = form["segYear"]; //seg tab segmentation year
var tractors = form["tractors"]; //seg tab tractors
var truck = form["trucks"]; //seg tab trucks
var su = form["serviceUnits"]; //seg tab service units
var estMileage = form["estAnnMile"]; // segmentation tab Est-Annual Mileage
var estRvenue = form["estAnnRev"]; //seg tab est annual revenue
var radius = form["radius"]; // seg tab radius ddl
var primaryOp = form["primaryOperation"]; //Seg tab primary operation ddl
int l, m, n, o, p = 0;
l = Convert.ToInt32(segYear); //Converts seg year into int
m = Convert.ToInt32(tractors); //converts seg tractors to int
n = Convert.ToInt32(truck); //converts seg trucks to int
o = Convert.ToInt32(su); //converts seg service units to int
p = Convert.ToInt32(estMileage); //converts seg est ann mileage to int
decimal revenue = Convert.ToDecimal(estRvenue); //converts seg est revenue to decimal
using (TruckingDb db = new TruckingDb())
{
// Create dto to insert Form collection items
CIF_Record_InsertDTO dto = db.CIF_Record.Find(u); //checking to see if row exists
CIF_Record_InsertDTO dtoSend = new CIF_Record_InsertDTO();
CIF_Segmentation_InsertDTO segDto = new CIF_Segmentation_InsertDTO();
if (dto == null)
{
// create UniqPolicy dto
dtoSend.UniqPolicyId = u;
// Create user insert dto.
string user = Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf(@"\") + 1);
dtoSend.Underwriter = user;
// set the cif record comments section.
dtoSend.Comments = psComments;
// Set the UwSegmentation value
dtoSend.UwSegmentationSubmit = true;
// Set segmentation dto's
segDto.SegmentationYear = l;
segDto.Tractors = m;
segDto.Trucks = n;
segDto.ServiceUnits = o;
segDto.EstAnnualMileage = p;
segDto.EstAnnualRevenue = revenue;
segDto.Radius = radius;
segDto.PrimaryOperation = primaryOp;
// 1. inserting dto's into two different tables
db.CIF_Record.Add(dtoSend);
db.CIF_Segmentation.Add(segDto); //2.
}
else
{
// temporarily empty
}
bool saveFailed;
do
{
saveFailed = false;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
//update original values from the db
var entry = ex.Entries.Single();
entry.OriginalValues.SetValues(entry.GetDatabaseValues());
}
} while (saveFailed);
}
return RedirectToAction("CompleteCIF");
}
do-while
仅高于他们的第一个修复,但它不起作用。点击catch并点击条目。GetDatabaseValues()
,但当然没有OG值。为什么saveChanges()
不起作用?再次感谢你的帮助 检查数据库中自动生成值的所有列是否在实体配置中标记为关键列的标识
,以及其他所有列的计算
。DateTime值可能与SQL类型和毫秒有关,所以检查它们是否为DateTime2(7)可能会有所帮助。根据表结构的不同,insert可能会导致insert+更新,这可能会触发并发异常,该异常只会发生在更新或删除中,而不会发生在insert中。当EF遇到一个默认列时,如果它没有被配置为考虑该列,则可能会将其混淆为并发更新
有关与日期时间相关的插入问题的详细信息,请参见()。在他们的情况下,需要确保将DB生成的选项设置为“无”。您的表单提交了两次吗?您是否尝试过不使用
DB.CIF\u Segmentation.Add(segDto)//2.
?(你没有在上面设置任何看起来像id的内容)另外,你的表中有datetime列吗?@tymtam我认为它不会提交两次,但我不能100%确定。我尝试过在没有db.CIF_Segmentation.Add(segDto)的情况下使用它,但这也不起作用。我让sql本身在插入每一新行时插入表的主键。所以没有必要用钥匙插入他的密码。只是填写行所需的值。是的,我在两个表中都有一个Datetime列。当您说将列标记为Identity或Computed时,您具体在哪里做?我的第一个想法是在模型中实例化所有dto变量,但这可能不正确。可能看起来像uniqId{get;set;}中的公共“Identity”从未尝试过这种方法,但我不确定在其他地方可能实际使用这些方法。可以使用实体中的属性、任何EntityTypeConfiguration定义(如果有)中的属性,或者在DbContext.OnModelCreating事件中完成。查看EF的DatabaseGenerateOptions设置,了解其使用位置和方式的示例。感谢您的帮助。我先从最简单的datetime开始,但我没有成功。事实证明,这与不通知EFF表的实际键未在模型中列出有关。我的印象是,因为SQL server自动递增了新插入的表行,所以不需要将其作为键包含。我还认为我在模型中输入的任何东西都必须包含在CRUD ops中。在这种情况下,情况并非如此。通过在模型中添加表主键,纠正了错误。