Question

I am using the following code to update clock in and clock out times in the record table but it doesn't save the data to the database. I checked the mdf file with sql management studio and there is no data saved. What am I doing wrong?

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();
            }
        }
    }

EDIT 1: Here is my app.config file (including what looks like a connection string)

<?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>
Était-ce utile?

La solution

As MM suggested in the comments, I also suspect that you're looking at the wrong MDF file the first time. With SQL Express those wizards tend to make copies of it and the final copy that the application is using is sometimes a different location than the one created by the wizard. The final one that is used is usually in the App_Data folder of your solution but you can trace in Debug in the save changes to be sure.

Here is an answer to a similar problem: Entity Framework 4.1 is not adding any rows to SQL Server Express database

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top