c# Linq - 这是删除记录的好设计吗?

发布于 2024-10-25 22:48:50 字数 684 浏览 1 评论 0原文

/// <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨落□心尘 2024-11-01 22:48:50

更好的方法是在 ArtworkDatasArtworkDataLabel 之间建立 FK 关系,并在数据库中设置级联删除 - 在这种情况下,您不必删除 tblArtworkDataLabels 根本没有,它会自动发生。

A better approach would be to have a FK relationship between ArtworkDatas and ArtworkDataLabel with a cascading delete set up in the DB - in that case you would not have to delete tblArtworkDataLabels at all, it would happen automatically.

幻梦 2024-11-01 22:48:50

您需要两次 DeleteAllOnSubmit(...) 调用,但只需要一次 Context.SubmitChanges()。

我还建议将您的上下文包含在 using(...) 子句中。

public static void DeleteDataRecord(int RecordID)
{
    using(var 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();
    }
}

YOu need two DeleteAllOnSubmit(...) calls, but only one Context.SubmitChanges().

I also suggest wrapping your context in a Using(...) clause.

public static void DeleteDataRecord(int RecordID)
{
    using(var 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();
    }
}
街道布景 2024-11-01 22:48:50

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文