繁体   English   中英

使用Linq-to-SQL在依赖主键/外键的多个表中插入记录

[英]Insert Records in multiple table which are dependent on primary / foreign key using Linq-to-SQL

我有3张桌子:

  1. master_upload (主上传具有主键,其自动增量名为master_upload_id)

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

  3. master_upload_tags (与秒相同)

在第二个和第三个表中,第一个表可以有多个行。

现在要插入第二张和第三张表,我需要一个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。

谢谢

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

试试这个代码:

// 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的魔力!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM