这是我写的代码。在 Quick Watch 中查看一切正常,但数据库未更新。
在更新数据.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" />