Efcore: SaveChangesAsync does not save changes to the database

Created on 5 Jun 2019  路  4Comments  路  Source: dotnet/efcore

SaveChangesAsync does not save changes made within the context to the database. No error's are noticed. However the changes cannot be seen in the database.

Exception message:
NA

Stack trace:
NA

I have attached the trace files.
efcore_sqlserver_new_trace.txt

Steps to reproduce:
```c#
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;
using System;

namespace CodeFirstAsync
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Program p = new Program();
p.Run();
}

public async void Run()
{
  try
  {
    using (var db = new BloggingContext())
    {
      await db.Database.EnsureDeletedAsync();
      await db.Database.EnsureCreatedAsync();
      var blog = new Instructor { Name = "async" };
      db.Instructors.Add(blog);
       await db.SaveChangesAsync();
    }
  }
  catch (Exception error)
  {
    Console.WriteLine(error.ToString());
  }
}

}

public class BloggingContext : DbContext
{
public static readonly LoggerFactory MyLoggerFactory
= new LoggerFactory(new[] { new DebugLoggerProvider((_, __) => true) });

public DbSet<Instructor> Instructors { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
  optionsBuilder.UseSqlServer(@"Data Source=YOUR DATA SOURCE;Initial Catalog=mynewdb;Integrated Security=True;Pooling=False").UseLoggerFactory(MyLoggerFactory);

  optionsBuilder.EnableSensitiveDataLogging();
}

}

public class Instructor
{
public int Id { get; set; }
public string Name { get; set; }
}
}

```

Further technical details

EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Microsoft Windows 10
IDE: Visual Studio 2017 15.9.11

closed-question customer-reported

All 4 comments

Looks like you are stiil doing async wrong

This is no place to learn C#. Please ask such questions on https://stackoverflow.com/ or similar websites. The EF Core team has more important things to do than answer such questions.

@pfdsilva Here's some code that works:
```C#
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
Program p = new Program();
await p.Run();
}

public async Task Run()
{
    try
    {
        using (var db = new BloggingContext())
        {
            await db.Database.EnsureDeletedAsync();
            await db.Database.EnsureCreatedAsync();
            var blog = new Instructor { Name = "async" };
            db.Instructors.Add(blog);
            await db.SaveChangesAsync();
        }
    }
    catch (Exception error)
    {
        Console.WriteLine(error.ToString());
    }
}

}
`` Notice that it uses anasync Task Mainmethod--you'll need C# 7 for this. It thenawaitsthe call toRun`, which in this case means the app won't try to exit while the async code is running.

Thanks @ajcvickers .

Was this page helpful?
0 / 5 - 0 ratings