用最简单的术语来说,当我尝试从POST数据更新我的对象Activities
时,关系Activities.Contacts
的现有多对多连接数据不会从MSSQL中删除SaveChanges
期间的数据库。如果该活动的POST数据中的某个联系人已存在,则会导致错误。
违反PRIMARY KEY约束' PK_tblActionContact'。无法在对象' dbo.tblActionContact'中插入重复键。重复键值为(1,94)
我的控制器:
public ActionResult Edit(Activity activity)
{
if (ModelState.IsValid)
{
//save the activity
activity.Contact.Clear(); //Clear the existing contacts
db.Entry(activity).State = EntityState.Modified;
//then add each contact
foreach (int contactId in activity.SelectedContactIds)
{
activity.Contact.Add(db.tblContact.Find(contactId));
}
db.SaveChanges();
TempData["Flash Success"] = "Activity Saved";
return RedirectToAction("Index", "Home");
}
return View(activity);
}
我已经尝试了很多不同的变化,基于对这个好网站的大量搜索,但我发现没有什么可行的。
我对activity.Contact.Clear()
抱有很高的期望,这确实清除了对象中活动的现有联系人,但它并没有从实际的MSSQL表中清除它们。
如果我选择的是未在现有Activity
中选择的新颖联系人,那么它会保存得很好,当然包括在创建新Activity
时。
拜托,我是新来发布的,所以如果需要更多代码或信息,请告诉我们!
修改 我发布了我的背景和适用的模型:
上下文:
public partial class SalesEntities : DbContext
{
public SalesEntities()
: base("name=SalesEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<SalesAccount> tblAccount { get; set; }
public virtual DbSet<Activity> tblAction { get; set; }
public virtual DbSet<ActionTypeLookup> tblActionTypeLookup { get; set; }
public virtual DbSet<Campaign> tblCampaign { get; set; }
public virtual DbSet<Contact> tblContact { get; set; }
public virtual DbSet<GenericLookup> tblGenericLookup { get; set; }
public virtual DbSet<MarketingLetter> tblMarketingLetter { get; set; }
public virtual DbSet<Opportunity> tblOpportunity { get; set; }
public virtual DbSet<Product> tblProduct { get; set; }
public virtual DbSet<Source> tblSource { get; set; }
public virtual DbSet<VerifiedAccount> tblVerifiedAccount { get; set; }
public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }
}
}
活动模型:
namespace SalesTracker.Models
{
using System;
using System.Collections.Generic;
public partial class Activity
{
public Activity()
{
this.Contact = new HashSet<Contact>();
}
public int ActionId { get; set; }
public int AccountId { get; set; }
public int CampaignId { get; set; }
public string SalespersonId { get; set; }
public int ActionTypeId { get; set; }
public string Note { get; set; }
public Nullable<System.DateTime> ActionDate { get; set; }
public string NextStep { get; set; }
public string Duration { get; set; }
public Nullable<int> MarketingLetterId { get; set; }
public string EditedBy { get; set; }
public bool Active { get; set; }
public System.DateTime EditDate { get; set; }
public bool Completed { get; set; }
public virtual SalesAccount SalesAccount { get; set; }
public virtual ActionTypeLookup ActionTypeLookup { get; set; }
public virtual Campaign Campaign { get; set; }
public virtual MarketingLetter MarketingLetter { get; set; }
public virtual ICollection<Contact> Contact { get; set; }
public virtual AspNetUsers AspNetUsers { get; set; }
public virtual AspNetUsers AspNetUsersEditedBy { get; set; }
}
}
和联系模式
namespace SalesTracker.Models
{
using System;
using System.Collections.Generic;
public partial class Contact
{
public Contact()
{
this.Activity = new HashSet<Activity>();
}
public int ContactId { get; set; }
public int AccountId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Note { get; set; }
public System.DateTime VerifiedDate { get; set; }
public string VerifiedBy { get; set; }
public int VerificationStatus { get; set; }
public System.DateTime EditDate { get; set; }
public string EditedBy { get; set; }
public bool Active { get; set; }
public virtual SalesAccount SalesAccount { get; set; }
public virtual ICollection<Activity> Activity { get; set; }
}
}
答案 0 :(得分:0)
我认为你对Clear()
的目的感到困惑。它所做的就是删除父项及其子项之间的引用,它实际上并没有删除实体。您将需要执行以下操作以实际删除它们:
activity.Contact.ToList().ForEach(c => db.Contact.DeleteObject(c));
当然遵循db.SaveChanges();