从记录中删除外键值时,我无法将记录保存到SQL Server数据库。
例如,我有一个主键BatchDetailID = 10的现有BatchDetail记录。它与现有的RunDetail记录相关联,该记录具有主键RunDetailID = 20和外键BatchDetailID = 10.我想要通过将RunDetails.BatchDetailID设置为null来解除RunDetail与BatchDetail的关联。
以下是错误消息:
发生了参照完整性约束违规:' BatchDetail.BatchDetailID'的属性值。在一段关系的一端与' RunDetail.BatchDetailID'的属性值不匹配。在另一端。
以下是我的课程定义:
public class RunDetail
{
public int RunDetailID { get; set; }
public Nullable<int> BatchDetailID { get; set; }
public virtual BatchDetail BatchDetail { get; set; }
}
public class BatchDetail
{
public int BatchDetailID { get; set; }
public virtual ICollection<RunDetail> RunDetails { get; set; }
}
当RunDetail
对象具有BatchDetailID
值时,会发生错误,我尝试将其设置为null:
public class MyController : Controller
{
private ISvr _myService;
public ISvr MyService
{
get { return _myService ?? (_myService = new MyHandler().GetMyService()); }
set
{
_myService = value;
}
}
public void UpdateBatch(int id)
{
foreach (var batchDetail in MyService.ReadBatchDetails(id))
{
var runDetail = MyService.ReadRunDetail(batchDetail.RunDetailID);
runDetail.BatchDetailID = null;
runDetail.BatchDetail = null; // I have tried with, and without, this line
MyService.UpdateRun(runDetail );
}
}
}
public class MyHandler
{
public ISvr GetMyService()
{
return new MySvr();
}
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)]
public class MySvr : IMySvr
{
public void UpdateRun(RunDetail runDetail)
{
using (var myEntities = new MyEntities())
{
myEntities.RunDetails.Attach(runDetail); // This is where the exception hits
myEntities.Entry(runDetail).State = EntityState.Modified;
myEntities.SaveChanges();
}
}
}
所有其他记录更新(包括将外键值更改为另一个)都能正常工作。
有人能看出我的缺陷是什么吗?
答案 0 :(得分:1)
问题根源于我在问题中没有包含的方法。当我检索到父BatchDetail
条记录时,我包含了&#34; RunDetails&#34;记录:
public List<BatchDetail> ReadBatchDetails(int id)
{
using (var myEntities = new MyEntities())
{
return myEntities.BatchDetails
.Include("RunDetails") // This is the problem
.AsNoTracking().ToList();
}
}
通过创建一个不Include("RunDetails")
的新方法,问题得以解决:
public List<BatchDetail> ReadBatchDetailsWithoutRunDetails(int id)
{
using (var myEntities = new MyEntities())
{
return myEntities.BatchDetails.AsNoTracking().ToList();
}
}
答案 1 :(得分:0)
我插入了1个BatchDetail和1个RunDetail,如下所示。
using (var db = new AppContext())
{
db.BatchDetails.Add(new BatchDetail { Id = 1 });
db.RunDetails.Add(new RunDetail { Id = 1, BatchDetailId = 1 });
db.SaveChanges();
}
然后这是抛出相同错误的结果。
using (var db = new AppContext())
{
// This is okay.
var x = new RunDetail { Id = 1, BatchDetailId = 1, BatchDetail = new BatchDetail { Id = 1 } };
db.RunDetails.Attach(x);
}
using (var db = new AppContext())
{
// This is okay.
var x = new RunDetail { Id = 1, BatchDetailId = null, BatchDetail = null };
db.RunDetails.Attach(x);
}
using (var db = new AppContext())
{
// This is okay.
var x = new RunDetail { Id = 1, BatchDetailId = 1, BatchDetail = null };
db.RunDetails.Attach(x);
}
using (var db = new AppContext())
{
// A referential integrity constraint violation occurred: The property
// value(s) of 'BatchDetail.Id' on one end of a relationship do not match the property
// value(s) of 'RunDetail.BatchDetailId' on the other end.
var x = new RunDetail { Id = 1, BatchDetailId = null, BatchDetail = new BatchDetail { Id = 1 } };
db.RunDetails.Attach(x);
}
在你的情况下应该是一样的。