Entity Framework 6 - Save Changes

asp.net-mvc entity-framework entity-framework-6 sql

Question

With EF 6, I'm attempting to perform an insert.

I can perform a read, which proves I have a connection to the database:

List<Driver> drivers = DataContext.Drivers.ToList();

I can see this perform a select on the database and return an item I manually inserted by using a sql profiler.

I'm attempting to insert something like this:

var driver = new Driver();
driver.DriverName = "Blah";
DataContext.Drivers.Add(driver);
DataContext.ChangeTracker.HasChanges(); //false
DataContext.SaveChanges();

except that nothing is added and the changetracker appears to be showing that no modifications have been found. I also saw advice on how to use.Attach but the outcomes were the same.

Any advice on where I might be going wrong?

Cheers


(MyEntities.Context.cs)

 public partial class MyEntities : DbContext
    {
        public MyEntities()
            : base("name=MyEntities")
        {
        }

public partial class MyDataContext : MyEntities
    {

public class SqlDataService : DataServiceBase<...Data.MyDataContext>
    {

//where I am trying to do the insert with the code above

edit: No, not that I'm aware of—not using code first! The above code samples don't necessarily assist, but they do demonstrate how I build up my classes.

1
4
4/23/2014 8:19:00 AM

Popular Answer

AutomaticTrackChanges appears to be disabled somewhere in your code. Before the addition, try adding the following line:

var driver = new Driver();
driver.DriverName = "Blah";
//Turning Automatic changes tracking on:
DataContext.Configuration.AutoDetectChangesEnabled = true;
DataContext.Drivers.Add(driver);
DataContext.ChangeTracker.HasChanges(); //True
DataContext.SaveChanges();

Note:AutoDetectChangesEnabled is typically disabled in favor of performance

1
8/7/2014 10:17:14 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow