我一直盯着这个,直到我看不清楚。我有一个控制台应用程序,我用它来测试逻辑输出,然后我将其转换为服务。从Web应用程序触发时,相同的代码工作正常。学生对象被填充正常,并且它没有抛出任何错误,但它从未保存到数据库。我对缺少什么的了解?
我的app.config文件有连接字符串
<connectionStrings>
<add name="DatabaseContext" connectionString="Data Source=|DataDirectory|Development.sdf" providerName="System.Data.SqlServerCe.4.0" />
<add name="DevelopmentEntities" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\Development.sdf"" providerName="System.Data.EntityClient" />
</connectionStrings>
foreach (DataRow dr in dt.Rows)
{
//fill student object and add to list
Student s = new Student();
s.SID = dr[0].ToString();
s.FirstName = dr[1].ToString();
s.LastName = dr[2].ToString();
if (sctType == "TRAD")
{ s.Type = Convert.ToInt32(UniversityGateway.Data.Models.StudentType.Traditional); }
else
{ s.Type = Convert.ToInt32(UniversityGateway.Data.Models.StudentType.AGS); }
s.Email = dr[5].ToString();
s.Address = new Address
{
Street = dr[6].ToString(),
City = dr[9].ToString(),
State = dr[10].ToString(),
Zip = dr[11].ToString(),
};
s.UserActive = "Y";
s.Status = Convert.ToInt32(UniversityGateway.Data.Models.StudentStatus.Pending);
s.Password = "kHPrSps3JnTkmky7PakBZg==Wdc9Qgcz39p+s6+fqk/nUw==";
students.Add(s);
using (var db = new UniversityGateway.Data.DatabaseContext())
{
db.Students.Add(s);
db.SaveChanges();
}
}
答案 0 :(得分:0)
原来它正在保存对象。只是连接到不同于连接字符串中指定的数据库。还有另外一个关于如何解决这个问题的问题。