c# Linq - 这是删除记录的好设计吗?
/// <summary>
/// Deletes a template data record
/// </summary>
/// <param name="RecordID">ID of the record</param>
public static void DeleteDataRecord(int RecordID)
{
ArtworkingDataContext dc = new ArtworkingDataContext();
// Delete associated datalabels
var q = dc.tblArtworkDataLabels.Where(c => c.dataID == RecordID);
dc.tblArtworkDataLabels.DeleteAllOnSubmit(q);
dc.SubmitChanges();
// Delete the data record
var qq = dc.tblArtworkDatas.Where(c => c.ID == RecordID);
dc.tblArtworkDatas.DeleteAllOnSubmit(qq);
dc.SubmitChanges();
}
我是否需要调用deleteallonsubmit()两次,还是可以只调用一次?
/// <summary>
/// Deletes a template data record
/// </summary>
/// <param name="RecordID">ID of the record</param>
public static void DeleteDataRecord(int RecordID)
{
ArtworkingDataContext dc = new ArtworkingDataContext();
// Delete associated datalabels
var q = dc.tblArtworkDataLabels.Where(c => c.dataID == RecordID);
dc.tblArtworkDataLabels.DeleteAllOnSubmit(q);
dc.SubmitChanges();
// Delete the data record
var qq = dc.tblArtworkDatas.Where(c => c.ID == RecordID);
dc.tblArtworkDatas.DeleteAllOnSubmit(qq);
dc.SubmitChanges();
}
Do I need to invoke deleteallonsubmit() twice, or can I have it just the once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更好的方法是在
ArtworkDatas
和ArtworkDataLabel
之间建立 FK 关系,并在数据库中设置级联删除 - 在这种情况下,您不必删除tblArtworkDataLabels 根本没有,它会自动发生。
A better approach would be to have a FK relationship between
ArtworkDatas
andArtworkDataLabel
with a cascading delete set up in the DB - in that case you would not have to deletetblArtworkDataLabels
at all, it would happen automatically.您需要两次 DeleteAllOnSubmit(...) 调用,但只需要一次 Context.SubmitChanges()。
我还建议将您的上下文包含在 using(...) 子句中。
YOu need two DeleteAllOnSubmit(...) calls, but only one Context.SubmitChanges().
I also suggest wrapping your context in a Using(...) clause.
q 和 qq 不是同一类型的对象,因此我相信您必须调用它两次。
您不必调用 dc.SubmitChanges() 两次。
此外,您可以设置数据库,以便 ArtworkData 和 ArtworkDataLabels 与级联删除相关。然后您可以删除主数据库,辅助数据库也将被删除。
q and qq are not the same type of objects, so you'd have to call it twice, I believe.
You don't have to call dc.SubmitChanges() twice.
Additionally, you could set up your DB so that ArtworkData and ArtworkDataLabels are related with a cascading delete. Then you could delete the primary and the secondary would be deleted as well.
不,删除是糟糕的设计
No, Deleting Is Bad Design