Corey
Corey

Reputation: 1793

How to use IdentityDbContext

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

Upvotes: 2

Views: 5447

Answers (1)

alsami
alsami

Reputation: 9815

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

Upvotes: 3

Related Questions