C
C#12mo ago
julian

EF Error when adding migration

I'm getting this error:
No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
I have defined this in the program.cs
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
This is my ApplicationDbContext.cs
public ApplicationDbContext(DbContextOptions options) : base(options) { }
public ApplicationDbContext(DbContextOptions options) : base(options) { }
10 Replies
WAASUL
WAASUL12mo ago
@Julian Can you share the entire ApplicationDbContext object?
julian
julianOP12mo ago
@SwaggerLife Sure!
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationDbContext()
{

}

public ApplicationDbContext(DbContextOptions options) : base(options) { }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserLogin<Guid>>()
.HasNoKey();
modelBuilder.Entity<IdentityUserRole<Guid>>()
.HasNoKey();
modelBuilder.Entity<IdentityUserToken<Guid>>()
.HasNoKey();
modelBuilder.Entity<StakedBottle>(entity =>
{
entity.Property(e => e.Keywords)
.HasConversion(
v => string.Join(',', v),
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(e => e.Trim()).ToArray()
);
});
modelBuilder.Entity<StakedBottle>()
.Property(e => e.Why)
.HasConversion(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<List<BottleWhyDetails>>(v) ?? new List<BottleWhyDetails>()
);
modelBuilder.Entity<StripePayment>();
}

public virtual DbSet<UserStakedBottle> UserBottles { get; set; }
public virtual DbSet<StakedBottle> StakeBottles { get; set; }
public virtual DbSet<Distillery> Distilleries { get; set; }
public virtual DbSet<StripePayment> StripePayments { get; set; }
public virtual DbSet<StripePromoCode> StripePromoCodes { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationDbContext()
{

}

public ApplicationDbContext(DbContextOptions options) : base(options) { }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserLogin<Guid>>()
.HasNoKey();
modelBuilder.Entity<IdentityUserRole<Guid>>()
.HasNoKey();
modelBuilder.Entity<IdentityUserToken<Guid>>()
.HasNoKey();
modelBuilder.Entity<StakedBottle>(entity =>
{
entity.Property(e => e.Keywords)
.HasConversion(
v => string.Join(',', v),
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(e => e.Trim()).ToArray()
);
});
modelBuilder.Entity<StakedBottle>()
.Property(e => e.Why)
.HasConversion(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<List<BottleWhyDetails>>(v) ?? new List<BottleWhyDetails>()
);
modelBuilder.Entity<StripePayment>();
}

public virtual DbSet<UserStakedBottle> UserBottles { get; set; }
public virtual DbSet<StakedBottle> StakeBottles { get; set; }
public virtual DbSet<Distillery> Distilleries { get; set; }
public virtual DbSet<StripePayment> StripePayments { get; set; }
public virtual DbSet<StripePromoCode> StripePromoCodes { get; set; }
}
WAASUL
WAASUL12mo ago
Try overriding the OnConfiguring method.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
Does this make any difference?
julian
julianOP12mo ago
Nope
Jimmacle
Jimmacle12mo ago
what if you change your constructor to accept DbContextOptions<ApplicationDbContext>?
WAASUL
WAASUL12mo ago
Ohh shit, yeah. Yeah you need that.
julian
julianOP12mo ago
I tried this:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DbName;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DbName;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
}
And this worked. But I'll try it out. Not my project, but I've used EF like 2 times.. I'm a Dapper user D:
Jimmacle
Jimmacle12mo ago
yeah don't do that, that's bad if you're using DI now you can only ever use your dbcontext with that connection string and it's hardcoded in your program
WAASUL
WAASUL12mo ago
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
// rest of your code
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
// rest of your code
}
JimTheDev1
JimTheDev112mo ago
See if this helps you. Near the bottom is code that resembles yours. https://stackoverflow.com/questions/19902756/asp-net-identity-dbcontext-confusion
Stack Overflow
ASP.NET Identity DbContext confusion
A default MVC 5 App comes with this piece of code in IdentityModels.cs - this piece of code is for all the ASP.NET Identity operations for the default templates: public class ApplicationDbContext :

Did you find this page helpful?