How to use IdentityDbContext

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

Question

For the last few days, I have been playing around with Asp.net's Identity framework.

I have been able to get the Register and Login working however when I try to extend the functionality to saving data against specific users, I find it is different to how I would normally implement it with stock standard EF.

Normally I would use something like below to save data:

using(var context = myDbContex())
{
   context.Add(object);
   context.SaveChanges();
}

However, when I try to use this approach after inheriting the IdentityDbContext it is expecting an argument. Is it okay for me to create a default constructor that doesn't take any arguments or should I be passing something in?

My Context currently looks like this:

public class AppContext : IdentityDbContext<ApplicationUser>
    {
        //I am not really sure why options needs to be specified as an argument   
        public AppContext(DbContextOptions<AppContext> options)
            : base(options)
        {
        }

        public DbSet<ApplicationUser> ApplicationUsers { get; set; }

        public DbSet<Xxxxx> Xxxxx { get; set; }

        public DbSet<Yyyyy> Yyyyy { get; set; }

        public DbSet<Zzzzz> Zzzzz { get; set; }

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

In Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<AppContext>(options =>
                                                        options.UseSqlite("Data Source=App.db"));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<AppContext>()
            .AddDefaultTokenProviders();
        });

Why is this implementation of the context different to the standard dbContext, and how can I save data using this context?

Thanks

1
1
3/12/2018 10:57:48 AM

Accepted Answer

Because of this line

services.AddDbContext<AppContext>(options => options.UseSqlite("DataSource=App.db"));

You need to provide a constructor that has the DbContextOptions as paramter, which has nothing todo with IdentityDbContext.

You have two choices now.

Use dependency injection, that is how you are supposed to use it anyway

public class MyController : Controller
{
   private AppContext context;

   public MyController(AppContext context)
   {
      this.context = context;
   }
}

Secondly you could register your context differently.

services.AddDbContext<AppContext>();

And apply changes in your context, remove the constructor and override OnConfiguring(DbContextOptionsBuilder optionsBuilder)

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
   optionsBuilder.UseSqlite("Data Source=App.db");
}

Now you can use it as you usually would do.

using(var context = new AppContext())
{
   // do stuff
}

EDIT:

Not part of the actual question but signin, registration and role managing is handled by these classes, that can be injected when using IdentityDbContext

2
3/12/2018 12:02:05 PM


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