Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

EF Core create instance in other thread and got NullReference when SaveChanges

$
0
0

Hi All!

I want to create background task with database access. I'll start my thread in Startup.cs in Configure method. 

with code: 

// create new database instance
var contextOptions = new DbContextOptionsBuilder<Data.ApplicationDbContext>().UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));

using (var context = new Data.ApplicationDbContext(contextOptions.Options))
{
   var mydata = context.Where(...);
   //.. work with mydata...
   await context.SaveChangesAsync();
}

And got NullReference Exception when call SaveChangesAsync(), whats wrong?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>