これは私が書いたコードです。クイック ウォッチに表示されているように、すべて正常に動作していますが、データベースは更新されていません。
IN updatedata.aspx.cs
using (var dc = new TrainingEntities())
{
var oldData = dc.tr_schools.ToList();
var newData = oldData.Select(o => new tr_schools()
{
banner = o.banner,
imageOff = o.imageOff,
imageOn = o.imageOn,
name = o.name,
schoolUID = o.schoolUID,
sequence = o.sequence,
title = o.title,
tr_modulesToSchools = o.tr_modulesToSchools,
type = o.type
}).ToList();
newData[0].name = newData[0].name + "XYZ";
newData[1].name = newData[1].name + "ABC";
var contents = dc.tr_Content.ToList();
foreach (var entryold in oldData)
{
foreach (var entrynew in newData)
{
if (entrynew.schoolUID == entryold.schoolUID && entrynew.name != entryold.name)
{
string filter = "-Courses- School " + entryold.name + " Banner Image";
var schoolContents = contents.Where(p => p.Label.Contains(filter));
foreach (var schoolContent in schoolContents)
{
schoolContent.Label = schoolContent.Label.Replace(entryold.name, entrynew.name);
}
}
}
}
dc.SaveChanges();
ここで行っているのは、データベースで古いラベルを見つけて新しいラベルに置き換えることで、ラベルを更新することです。
私の接続文字列:
<connectionStrings>
<add name="TrainingConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=ABCD;User ID=***;Password=*****;" providerName="System.Data.SqlClient" />
<add name="TrainingEntities" connectionString="metadata=res://*/Models.DAL.TrainingEntities.csdl|res://*/Models.DAL.TrainingEntities.ssdl|res://*/Models.DAL.TrainingEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=ABCD;user id=***;password=*****;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />