我使用以下代码更新记录表中的时钟输入和时钟输出时间,但它不会将数据保存到数据库中。我用sql management studio检查了mdf文件,没有保存数据。我做错了什么?
private void btClockIn_Click(object sender, EventArgs e)
{
using (TimeKeepEntities ctx = new TimeKeepEntities())
{
// if employee clocked out add new clock in record else update existing record with clock out time
if (cmbProjects.Enabled)
{
tblTimeRecord record = new tblTimeRecord();
record.RecordID = DateTime.Now;
record.EmployeeID = (int)cmbEmployees.SelectedValue;
record.ProjectID = (int)cmbProjects.SelectedValue;
record.ClockIn = DateTime.Now;
ctx.tblTimeRecords.Add(record);
ctx.SaveChanges();
update_cmbProjects_btClockIn();
}
else
{
tblTimeRecord record = (from r in ctx.tblTimeRecords
where r.EmployeeID == (int)cmbEmployees.SelectedValue && !r.ClockOut.HasValue
select r).FirstOrDefault();
record.ClockOut = DateTime.Now;
ctx.SaveChanges();
update_cmbProjects_btClockIn();
}
}
}
编辑1:这是我的app.config文件(包括看起来像连接字符串的内容)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="TimeKeepEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\TimeKeep.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
答案 0 :(得分:1)
正如MM在评论中所说,我也怀疑你第一次看错了MDF文件。使用SQL Express,这些向导倾向于复制它,而应用程序使用的最终副本有时与向导创建的位置不同。最后一个使用的通常是在解决方案的App_Data文件夹中,但您可以在保存更改中跟踪调试,以确保。
以下是对类似问题的回答:Entity Framework 4.1 is not adding any rows to SQL Server Express database