将数据从一个实体移动到另一个实体

时间:2012-03-29 08:49:03

标签: wpf entity-framework linq-to-entities

我有2个实体文档(Id,Number,Content,Date_Added)和Adocuments(文档档案)(Ida,Number,Content,Date_Added),我想移动文档(例如)来自我的档案实体的三月。我的表格有相同的字段。

选择部分

WPF_TestEntities WPFModel;
DateTime init_per = new DateTime(2012, 03, 01);
DateTime fina_per = new DateTime(2012, 03, 31);
var qry = from d in WPFModel.Document // qry - documents form march
          where d.Date_Added >= init_per &&  d.Date_Added <= fina_per
          select d;

插入部分

//WPFModel.Adocument.Insert/Add(qry);

之后,我可以从Document实体中删除march文档。

如何将数据(qry)从Documents移动到Adocuments? (我的Id的唯一性会有任何问题吗?)

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

 WPF_TestEntities WPFModel;
 DateTime init_per = new DateTime(2012, 03, 01);
 DateTime fina_per = new DateTime(2012, 03, 31);
 var qry = from d in WPFModel.Document // qry - documents form march
      where d.Date_Added >= init_per &&  d.Date_Added <= fina_per
      select d;

 foreach (Document doc in qryluna)
     {
      Adocument newadoc = new Adocument();
      newadoc.IdA = doc.Id;
      newadoc.Nr_intern = doc.Nr_intern;
      newadoc.Obiect = doc.Obiect;
      newadoc.Data_Added = doc.Data_Added ;
      WPFModel.Adocument.AddObject(newadoc);
      WPFModel.Document.DeleteObject(doc);
  }
  WPFModel.SaveChanges();

答案 1 :(得分:0)

你可以尝试向下转换查询中的元素结果,如果它不运行你应该把每个元素复制到一个新的对象,而不是将它保存在hystory表中。 id可能是个问题,因为如果你将id作为autoincrement(sql server上的身份)确定,那么db当然会尝试释放一个新的id,而不是你的旧id。 您应该做的最安全的任务是查询第一个实体,获取每个值并填充一个新的hystorical实体(与hystory表相关)并保存它。 我想你可以在第二个表格(Adocuments)中找到这些字段:

  • id(您的表身份ID)
  • ida(您的ID从Documents表中复制,以真正维护历史记录)
  • 含量
  • DATE_ADDED

我希望这对你有用:)

相关问题