Getting metadata in EF Core: table and column mappings

Is this metadata available anywhere in EF Core? Yes it is. Just additionally to the properties examine the methods (GetXXX, FindXXX etc.). And pay special attention to Relational() extension methods. For instance: foreach (var entityType in dbContext.Model.GetEntityTypes()) { var tableName = entityType.Relational().TableName; foreach (var propertyType in entityType.GetProperties()) { var columnName = propertyType.Relational().ColumnName; } } You … Read more

How should I manage DbContext Lifetime in MVC Core?

As others already explained, you should use a scoped dependency for database contexts to make sure it will be properly reused. For concurrency, remember that you can query the database asynchronously too, so you might not need actual threads. If you do need threads, i.e. background workers, then it’s likely that those will have a … Read more

How to get user information in DbContext using Net Core

I implemented an approach similar to this that is covered in this blog post and basically involves creating a service that will use dependency injection to inject the HttpContext (and underlying user information) into a particular context, or however you would prefer to use it. A very basic implementation might look something like this: public … Read more

How to load navigation properties on an IdentityUser with UserManager

Unfortunately, you have to either do it manually or create your own IUserStore<IdentityUser> where you load related data in the FindByEmailAsync method: public class MyStore : IUserStore<IdentityUser>, // the rest of the interfaces { // … implement the dozens of methods public async Task<IdentityUser> FindByEmailAsync(string normalizedEmail, CancellationToken token) { return await context.Users .Include(x => x.Address) … Read more

DbSet doesn’t have a Find method in EF7

Here’s a very crude, incomplete, and untested implementation of .Find() as an extension method. If nothing else, it should get you pointed in the right direction. The real implementation is tracked by #797. static TEntity Find<TEntity>(this DbSet<TEntity> set, params object[] keyValues) where TEntity : class { var context = ((IAccessor<IServiceProvider>)set).Service.GetService<DbContext>(); var entityType = context.Model.GetEntityType(typeof(TEntity)); var … Read more

What goes into DbContextOptions when invoking a new DbContext?

If you really want to create the context manually, then you can configure it like this: var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>(); optionsBuilder.UseSqlServer(Configuration.GetConnectionStringSecureValue(“DefaultConnection”)); _context = new ApplicationDbContext(optionsBuilder.Options); (The DbContextOptionsBuilder<ApplicationDbContext> class is the type of options argument in services.AddDbContext<ApplicationDbContext>(options =>). But in the controller, you don’t have access to Configuration object, so you would have to expose … Read more

Entity Framework Core: `SqlNullValueException: Data is Null.` How to troubleshoot?

The error message indicates that EF Core is trying to read string value for a required property, i.e. a property which should never have null value in the database, but instead the underlying data reader reports null value for that property in some record(s). Looking at your entity model and corresponding database table, you can … Read more