【问题标题】:Insert Records in multiple table which are dependent on primary / foreign key using Linq-to-SQL使用 Linq-to-SQL 在多个表中插入依赖于主键/外键的记录
【发布时间】:2015-04-25 06:30:12
【问题描述】:

我有 3 张桌子:

  1. master_upload(主上传具有名为 master_upload_id 的自动递增主键)

  2. master_upload_files(由2列组成,参考上表中的master_upload_id)

  3. master_upload_tags(同第二个)

在第 2 表和第 3 表中,第 1 表可以有多行。

现在要插入第二张和第三张表,我需要一个 master_upload_id,我只有在插入后才能得到它。因此,我必须至少致电db.SubmitChanges 3 次。如果第二个和第三个表有多个值,我必须为这两个表中的每一行调用db.SubmitChanges。但有时由于某些规则违规,在第二或第三表中的插入可能会失败。

因此,在这些情况下我需要回滚。我该怎么做?

我以前通过 SQL Server 存储过程来完成这些事情,但现在我需要在 LINQ 中完成。

// Here is my code sample
using (dbDataContext db = new dbDataContext())
{
    db.master_uploads.InsertOnSubmit(mu);// toget mu.upload_id

    try
    {
        db.SubmitChanges();
        master_upload_file mf = new master_upload_file();
        mf.master_upload_id = mu.upload_id;
        mf.upload_file_id = uploadedfile.file_id;

        db.master_upload_files.InsertOnSubmit(mf);

        for (int i = 0; i < tags.Length; i++)
        {
            master_upload_tag mt = new master_upload_tag();
            mt.master_upload_id = mu.upload_id;
            mt.tag = tags[i];
            db.master_upload_tags.InsertOnSubmit(mt);
        }

        db.SubmitChanges();
        gtu.writetext("0",context);
    }
    catch (Exception)
    {
        gtu.writetext("1:File Upload Add Error", context);
    }
}

我使用的是 SQL Server 2008。

谢谢

【问题讨论】:

  • 你现在应该知道必须显示代码了。
  • 我已添加代码
  • TransactionScope 应该会有所帮助。您需要运行分布式事务协调器服务,但我认为这就是您要寻找的。​​span>

标签: c# sql-server database linq


【解决方案1】:

你这样做太复杂了!使用武力,伙计! :-)

试试这个代码:

// define your context
using (UploadContextDataContext ctx = new UploadContextDataContext())
{
    try
    {
        // create your new upload
        upload up = new upload();
        up.UploadName = "Some test name";

        // define two new upload files 
        UploadFile file1 = new UploadFile { FileName = "TestFile1.zip" };
        UploadFile file2 = new UploadFile { FileName = "TestFile2.zip" };

        // *ADD* those two new upload files to the "Upload"
        up.UploadFiles.Add(file1);
        up.UploadFiles.Add(file2);

        // define three new upload tags
        UploadTag tag = new UploadTag { TagName = "Tag #1" };
        UploadTag tag2 = new UploadTag { TagName = "Tag #2" };
        UploadTag tag3 = new UploadTag { TagName = "Tag #3" };

        // *ADD* those three new upload tags to the "Upload"
        up.UploadTags.Add(tag);
        up.UploadTags.Add(tag2);
        up.UploadTags.Add(tag3);

        // add the "Upload" to the context - this *INCLUDES* the files and tags!
        ctx.uploads.InsertOnSubmit(up);

        // call SubmitChanges *just once* to store everything!
        ctx.SubmitChanges();
    }
    catch (Exception exc)
    {
        string msg = exc.GetType().Name + ": " + exc.Message;
    }

您基本上设置了一个对象图-您的基本Upload 对象-然后您将文件和标签添加到该Upload 对象。您只需要将 Upload 对象添加到数据上下文中 - 其他(添加的)子对象会自动标记!

在这种情况下,您只需要调用SubmitChanges() 一次,这将插入所有新对象,设置所有必要的外键/主键关系,一切都为您服务.无需摆弄主键,多次调用数据库 - 只需使用 Linq-to-SQL 的魔力!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多