0

我正在使用以下代码更新记录表中的打卡和打卡时间,但它不会将数据保存到数据库中。我用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=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\TimeKeep.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>
4

1 回答 1

1

正如 MM 在评论中所建议的那样,我也怀疑您第一次查看的 MDF 文件是错误的。使用 SQL Express,这些向导往往会对其进行复制,并且应用程序使用的最终副本有时与向导创建的位置不同。使用的最后一个通常位于解决方案的 App_Data 文件夹中,但您可以在保存更改的 Debug 中进行跟踪以确保。

这是一个类似问题的答案:Entity Framework 4.1 is not added any rows to SQL Server Express database

于 2012-09-20T00:48:57.763 回答