ActiveRecord 切换数据库保存数据(二)
如果 数据表之间已经设置了关联:
1. 用ActiveWriter 设置了 SourceColumn,TargetColumnKey,TagertCascase,TargetLazy ,Target Inverse
2.设置version
需要用下面方法更新(数据库里完全没有数据的情况)
///// TABLE2 是Table1的子表
List<Standard> standards = new List<Standard>(Standard.FindAll());
// System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
// con.ConnectionString = "Data Source=200.200.200.20;Initial Catalog=FMEA1;Integrated Security=SSPI";
// using (new Castle.ActiveRecord.Framework.Scopes.DifferentDatabaseScope(con))
// {
// foreach (Standard s in standards)
// {
// s.Update();
// }
// }
// List<Express> levels = new List<Express>(Express.FindAll());
//List<TABLE1> listTable1 = new List<TABLE1>(TABLE1.FindAll());
//List<TABLE2> listTable2 = new List<TABLE2>(TABLE2.FindAll());
ActiveRecordStarter.ResetInitializationFlag();
IConfigurationSource source2 = System.Configuration.ConfigurationManager.GetSection("access") as IConfigurationSource;
//开启日志
//log4net.Config.XmlConfigurator.Configure();
Assembly assembly = Assembly.Load(@"DBChange");
ActiveRecordStarter.Initialize(assembly, source2);
////// List<Express> levels = new List<Express>(Express.FindAll());
try
{
using (TransactionScope ts = new TransactionScope())
{
try
{
foreach (Standard sd in standards)
{
sd.SaveCopy();
}
ts.VoteCommit();
}
catch
{
ts.VoteRollBack();
}
}
}
catch
{
MessageBox.Show("失败");
}
MessageBox.Show("成功");