Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time

Tags:

I face the following error when adding the migration of database in .net core

This is the error:

This is the code in Startup:

public void ConfigureServices(IServiceCollection services) {     services.AddDbContext<ApplicationDbContext>(options =>      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));                 services.AddDefaultIdentity<ApplicationUser>().AddEntityFrameworkStores<ApplicationDbContext>();     services.AddControllers(); } 

This is the ApplicationDbContext class:

public class ApplicationDbContext : IdentityDbContext {     public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)     { }      public DbSet<ApplicationUser> applicationUsers { get; set; } } 

This is the ApplicationUser:

public class ApplicationUser : IdentityUser {     [Required]     [Column(TypeName = "nvarchar(150)")]     public string UserFName { get; set; }     [Required]     public string UserLName { get; set; } } 
like image 853
eman Avatar asked Jan 18 '20 00:01

eman


2 Answers

I found the cause of this error could be multiple things in your code. For me at least, the best way was to add verbose in command.

With that will be able to understand what is the problem. the verbose will display all steps of the execution.

In visual studio use:

add-migration Added_something -verbose 

For the CLI use:

dotnet ef migrations add Added_something  --verbose 
like image 93
AFetter Avatar answered Sep 20 '22 09:09

AFetter


This error can also occur if multiple startup projects is selected. I set my webproject to startup project and that solved the issue for me.

like image 43
Pelle Åhlander Avatar answered Sep 17 '22 09:09

Pelle Åhlander