0
我试图使用实体框架从一个数据库获取数据(嵌套关系)并将其插入另一个数据库。使用实体框架跨数据库复制数据
这个想法是你将数据下载为一个XML文件,然后将其上传到新的数据库中。
下载到XML很简单:
var BoroughQuery = from b in Context.Borough.Include("User").Include("Location").Include("Contact").Include("Location.LocationContact") where b.Id == BoroughId select b;
BoroughModel = BoroughQuery.First();
Context.Detach(BoroughModel);
System.Runtime.Serialization.DataContractSerializer ser = new System.Runtime.Serialization.DataContractSerializer(typeof(Borough));
System.IO.DirectoryInfo TempDirectory = new System.IO.DirectoryInfo(Server.MapPath(Request.ApplicationPath + "Temp"));
if (!TempDirectory.Exists) TempDirectory.Create();
System.IO.FileInfo TempFile = new System.IO.FileInfo(TempDirectory.FullName + "\\" + BoroughModel.Key + "_" + System.DateTime.UtcNow.ToString("yyyyMMddhhmmss") + ".xml");
FileStream writer = new FileStream(TempFile.FullName, FileMode.Create);
ser.WriteObject(writer, BoroughModel);
writer.Close();
问题 ..是连接读取XML到另一个上下文,并保存到数据库
到目前为止,我有以下哪些从XML中读取模型的罚款,但结果没有任何东西被添加到分贝:
DataContractSerializer ser = new DataContractSerializer(typeof(Borough));
Borough deserializedBorough = (Borough)ser.ReadObject(fleBoroughUpload.FileContent);
using (Entities Context = new Entities("name=EntitiesTarget"))
{
Context.Attach(deserializedBorough);
Context.SaveChanges();
}
任何帮助将不胜感激。
注意 因为这两个数据块是数据没有下来作为XML在同一台服务器上,但我想这个问题仍然是相同的。