Entity Framework 6 - Save Changes

I am trying to perform an insert with EF 6.

I checked that I have a database connection because I can read:

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

With sql profiler, I see that it makes a selection in the database and returns the item that I manually inserted.

I am trying to do an insert like this:

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

but nothing is inserted, and changetracker seems to indicate that it has not detected any changes. I also saw suggestions to use .Attach, but it had the same results.

Any help on what I'm doing wrong?

Greetings


(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: Don't use the code first (not what I know!), not sure if the above code examples help, but show how I set up my classes

+4
1

, AutomaticTrackChanges - . :

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

. AutoDetectChangesEnabled

+1

Source: https://habr.com/ru/post/1537647/


All Articles