繁体   English   中英

实体框架不会将更新保存到基础数据库

[英]Entity Framework doesn't save updates to the underlying database

我正在使用以下代码来更新记录表中的切入和切出时间,但是它没有将数据保存到数据库中。 我用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>

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

这是对类似问题的解答: Entity Framework 4.1不会向SQL Server Express数据库添加任何行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM