Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用LINQ将临时表迁移到SQL?_C#_Linq_Linq To Sql - Fatal编程技术网

C# 如何使用LINQ将临时表迁移到SQL?

C# 如何使用LINQ将临时表迁移到SQL?,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有两张表:contacts和contact_temps。contact_temps表镜像contacts表。我想做的只是从临时表中提取记录并将其插入联系人中。之后,我将从contact_temps表中删除这些记录 下面的代码只迁移一条记录,不从临时表中删除任何内容。如何解决我的问题?谢谢 // migrate temp profile(s)... var tempProfilesToMigrate = from ct in db.contact_

我有两张表:contacts和contact_temps。contact_temps表镜像contacts表。我想做的只是从临时表中提取记录并将其插入联系人中。之后,我将从contact_temps表中删除这些记录

下面的代码只迁移一条记录,不从临时表中删除任何内容。如何解决我的问题?谢谢

            // migrate temp profile(s)...
            var tempProfilesToMigrate = from ct in db.contact_temps
                                         where ct.SessionKey == contact.Profile.SessionId
                                         select new contact();



            db.contacts.InsertAllOnSubmit(tempProfilesToMigrate);
            db.SubmitChanges();

            //...clear temp table records
            var tempProfilesToDelete = from ct in db.contact_temps
                                        where ct.SessionKey == contact.Profile.SessionId
                                        select ct;

            db.contact_temps.DeleteAllOnSubmit(tempProfilesToDelete);
            db.SubmitChanges();

var result=db.ExecuteCommand(“插入到联系人中,从联系人中选择*,其中SessionKey={0}”,contact.Profile.SessionId)

当然,这只是我的想法,但你明白了。更好的做法是将迁移和删除放在存储过程中。您使用的方法将对所有联系人临时记录进行两次往返(一次用于插入,一次用于删除)

顺便说一句,谷歌“编码优先存储过程”提供了一种使用EF 4.1调用存储过程的方法,我想知道您的“提交时全部插入”是否导致实体与db.contacts关联。试试这个

// migrate temp profile(s)...
var tempProfiles = from ct in db.contact_temps
                             where ct.SessionKey == contact.Profile.SessionId
                             select ct;

foreach (var c in tempProfiles)
{
    Contact newC = new Contact();
    newC.Name = c.Name;
    // copy other values

    db.contacts.InsertOnSubmit(newC);
}

// WAIT! do it at once in a single TX => avoid db.SubmitChanges() here.

 db.contact_temps.DeleteAllOnSubmit(tempProfiles);

 // Both sets of changes in one Tx.
 db.SubmitChanges();
您还可以编写一个存储过程并将其导入到db上下文中,然后调用它