在我们的SQL数据库中,我们有一个带有组合键的表格,其中包含我们需要随时更新的字段。这是我的理解,因为我们正在使用实体框架,我需要先从数据库中删除记录,然后将该行添加回表中。使用linq更新复合键字段
以下是我创建的用于处理“更新”的简单方法,因为有许多方法将执行此操作。然而,一旦.SaveChanges()
方法是.Remove
后叫我拿到DbUpdateConcurrencyException
:
Store update, insert, or delete statement affected an unexpected number of
rows (0). Entities may have been modified or deleted since entities were loaded.
Refresh ObjectStateManager entries.
不知道我在做什么错了,因为我想删除的记录,然后进行更新,然后添加记录背部。
下面是调用remove/edit方法的方法。当这个方法被调用时,记录没有以任何形式或形式改变。
private static void ProcessAllChanges(ZipCodeIndex information, ZipCodeTerritory zipToUpdate)
{
try
{
RemoveRecord(zipToUpdate);
if (!string.IsNullOrWhiteSpace(information.newTerritory)) zipToUpdate.IndDistrnId = information.newTerritory;
if (!string.IsNullOrWhiteSpace(information.newStateCode)) zipToUpdate.StateCode = information.newStateCode;
if (!string.IsNullOrWhiteSpace(information.newDescription)) zipToUpdate.DrmTerrDesc = information.newDescription;
if (!string.IsNullOrWhiteSpace(information.newChannelCode)) zipToUpdate.ChannelCode = information.newChannelCode;
if (zipToUpdate.EndDate == DateTime.MinValue) zipToUpdate.EndDate = DateTime.MaxValue;
EditRecord(zipToUpdate);
_updated++;
}
catch (DbEntityValidationException dbEx)
{
_msg += "Error during update; ";
EventLog.WriteEntry("Monet", "Error during ProcessAllChanges: " + zipToUpdate.ToString() + " |EX| " + dbEx.Message);
}
catch (Exception ex)
{
_msg += "Error during update; ";
EventLog.WriteEntry("Monet", "Error during ProcessAllChanges: " + zipToUpdate.ToString() + " |MESSAGE| " + ex.Message);
}
}
而且这里有两个helper方法被调用
public static void RemoveRecord(ZipCodeTerritory zipCode)
{
_db = new AgentResources();
_db.ZipCodeTerritory.Attach(zipCode);
_db.ZipCodeTerritory.Remove(zipCode);
_db.SaveChanges();
}
public static void EditRecord(ZipCodeTerritory zipCode)
{
_db = new AgentResources();
_db.ZipCodeTerritory.Add(zipCode);
_db.SaveChanges();
}
编辑
基于一对夫妇的评论下面我试图创建上下文对象的一个单独的实例,但是我使用这种方法收到了同样的错误:
public static void RemoveRecord(ZipCodeTerritory zipCode)
{
using (AgentResources deleteMe = new AgentResources())
{
deleteMe.ZipCodeTerritory.Attach(zipCode);
deleteMe.ZipCodeTerritory.Remove(zipCode);
deleteMe.SaveChanges();
}
}
第二编辑
这是最上面的方法,它调用上面发布的ProcessAllChanges
方法。
public static string TerritoryOnly(ZipCodeIndex updateZip)
{
if (!string.IsNullOrWhiteSpace(updateZip.newEffectiveDate) || !string.IsNullOrWhiteSpace(updateZip.newEndDate))
{
return "Neither effective or end date can be present if updating Territory Code only; ";
}
RefreshProperties();
foreach (var zipCode in updateZip.displayForPaging.Where(x => x.Update))
{
ProcessAllChanges(updateZip, zipCode);
}
_msg += _updated + " record(s) updated; ";
return _msg;
}
第三编辑
每请求这里是AgentResources
完整的类定义,我们DbContext
对象
namespace Monet.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class AgentResources : DbContext
{
public AgentResources()
: base("name=AgentResources")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<AgentContEd> AgentContEd { get; set; }
public DbSet<ContEdCourse> ContEdCourse { get; set; }
public DbSet<Course> Course { get; set; }
public DbSet<CourseToProduct> CourseToProduct { get; set; }
public DbSet<Product> Product { get; set; }
public DbSet<ProcessControl> ProcessControl { get; set; }
public DbSet<AgentIdToTradingPartner> AgentIdToTradingPartner { get; set; }
public DbSet<TradingPartner> TradingPartner { get; set; }
public DbSet<Notes> Notes { get; set; }
public DbSet<CourseMaterials> CourseMaterials { get; set; }
public DbSet<TransactionLog> TransactionLog { get; set; }
public DbSet<Agent> Agent { get; set; }
public DbSet<AgentIdentification> AgentIdentification { get; set; }
public DbSet<BatchDashboard> BatchDashboard { get; set; }
public DbSet<BatchPrograms> BatchPrograms { get; set; }
public DbSet<FollowUpItems> FollowUpItems { get; set; }
public DbSet<sysdiagrams> sysdiagrams { get; set; }
public DbSet<AgentProductTraining> AgentProductTraining { get; set; }
public DbSet<Channel> Channel { get; set; }
public DbSet<RelationshipCodes> RelationshipCodes { get; set; }
public DbSet<DropDownValues> DropDownValues { get; set; }
public DbSet<QueueUpdates> QueueUpdates { get; set; }
public DbSet<MarketingLookup> MarketingLookup { get; set; }
public DbSet<TransmissionHistory> TransmissionHistory { get; set; }
public DbSet<AgentTransmission> AgentTransmission { get; set; }
public DbSet<ZipCodeTerritory> ZipCodeTerritory { get; set; }
}
}
这里是ZipCodeTerritory
类
public partial class ZipCodeTerritory
{
public string ChannelCode { get; set; } //Composite key field
public string DrmTerrDesc { get; set; }
public string IndDistrnId { get; set; }
public string StateCode { get; set; } //Composite key field
public string ZipCode { get; set; } //Composite key field
public System.DateTime? DisplayEndDate { get; set; }
public System.DateTime EndDate { get; set; } //Composite key field
public System.DateTime EffectiveDate { get; set; }
public string LastUpdateId { get; set; }
public Nullable<System.DateTime> LastUpdateDate { get; set; }
}
它看起来像你离开你的背景下开'_db'但随后在你的辅助方法,每次调用构造函数'_db =新AgentResources();'你为什么这样做?很难说,因为你已经留下了大部分代码。你在同一班上表现出的一切吗?为什么是静态方法? – EkoostikMartin
这些方法是静态的,因为它们是公共助手类的一部分。网页上可能会出现许多更新,因此我们决定使用单独的.cs文件清理代码。这些方法只是将模型对象传递给它('ZipCodeTerritory'模型)。 – NealR
这也是为什么上下文在每次使用时都调用构造函数的原因。我们希望确保每次都使用干净的平板,而不是以前的交易。 – NealR