I have just created a brand new core MVC app with identity. I can't for the life of me figure out how to get entity framework to do a database query.
I have an ApplicationDBContext
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
whenever I try to use it it just tells me i have no options sent.
I'm trying to run a query on my UserAddresses table and have changed my ASPNetUsers table to Users.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
// Update the ApplicationUser Table to Users and change primary column to UserId
builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable("Users");
entity.Property(e => e.Id).HasColumnName("UserId");
});
}
public DbSet<UserAddress> UserAddresses { get; set; }
This is the entire Class
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
// Update the ApplicationUser Table to Users and change primary column to UserId
builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable("Users");
entity.Property(e => e.Id).HasColumnName("UserId");
});
}
public DbSet<UserAddress> UserAddresses { get; set; }
}
How do I connect to the DBContext to run queries using EF
Update
I was able to get it by passing my _context
into the controller through a constructor like this
private ApplicationDbContext _context;
public HomeController(ApplicationDbContext context)
{
_context = context;
}
IdentityDbContext<ApplicationUser>
to justDbContext
?_context
.