Database Query with EF Core in 2.1

asp.net-core c# database entity-framework-core

Question

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;
    }
1
0
10/13/2018 9:30:36 AM

Popular Answer

private ApplicationDbContext _context;
public HomeController(ApplicationDbContext context)
{
    _context = context;
}

// this will get the userId of currently loggedIn user
userId = User.Identity.GetUserId()

// this line get all the address of loggedIn user from UserAddresses table
_context.UserAddresses.Where(a => a.UserId == userId).ToList();

You cal also get single address, first address or last address etc.

Configuration in Startup.cs

services.AddIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<AppIdentityDbContext>()
            .AddDefaultTokenProviders();
0
10/13/2018 8:15:47 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow