Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than one DbContext was found

I am implementing a code first database using AspCore 2. I have a "DataContext.cs" that goes like this:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string MiddelName { get; set; }
    public string LastName { get; set; }
    public bool IsActive { get; set; }
    public DateTime? DateAdded { get; set; }
}

public class DataContext : IdentityDbContext<ApplicationUser>
{
    public DataContext(DbContextOptions<DataContext> options) : base(options) {}

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             base.OnModelCreating(modelBuilder);

          //AspNetUsers -> User
        modelBuilder.Entity<ApplicationUser>()
            .ToTable("User");
        //AspNetRoles -> Role
        modelBuilder.Entity<IdentityRole>()
            .ToTable("Role");
        //AspNetUserRoles -> UserRole
        modelBuilder.Entity<IdentityUserRole>()
            .ToTable("UserRole");
        //AspNetUserClaims -> UserClaim
        modelBuilder.Entity<IdentityUserClaim>()
            .ToTable("UserClaim");
        //AspNetUserLogins -> UserLogin
        modelBuilder.Entity<IdentityUserLogin>()
            .ToTable("UserLogin");
    }
}

and this in my "startup.cs"

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }
}

When I try running the dotnet migration, dotnet ef migrations add InitialCreate I get the following error:

"More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands."

Can you please help me make this right? Thank you!

like image 319
Ibanez1408 Avatar asked Sep 13 '18 09:09

Ibanez1408


People also ask

Can we have multiple DbContext?

Entity Framework : A Comprehensive CourseMultiple DbContext was first introduced in Entity Framework 6.0. Multiple context classes may belong to a single database or two different databases.

Can we have multiple DbContext in Entity Framework?

In code first, you can have multiple DBContext and just one database. You just have to specify the connection string in the constructor. Yes you can, but how can you query from different entities from different db contexts?

How do I get rid of migration?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

What is DbContext?

A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.


3 Answers

It looks like there are several classes that have been inherited from DbContext class (may have come from some NuGet package). So add migration with

Add-Migration MyMigration -context DataContextName
like image 88
maximelian1986 Avatar answered Oct 06 '22 11:10

maximelian1986


please follow this syntax

Add-Migration [-Name] <String> [-OutputDir <String>] [-Context <String>] [-Project <String>] [-StartupProject <String>] [-Environment <String>] [<CommonParameters>]

in your case,

add-migration MyMigration -Context DataContext
like image 44
Daleman Avatar answered Oct 06 '22 11:10

Daleman


dotnet ef migrations add <your_migration_name> -c <your_context_class_name>

[--context | -c]

The DbContext class to use. Class name only or fully qualified with namespaces. If this option is omitted, EF Core will find the context class. If there are multiple context classes, this option is required.

from https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#common-options

like image 29
themefield Avatar answered Oct 06 '22 12:10

themefield