locked
Two migration sets with two DBContext generate an error RRS feed

  • Question

  • User78124615 posted

    I am working with ef core 3.1.4 and .net core 3.1.4, and trying to implement migration with two different database providers using two migration sets, I followed the documentation, and added a new DbContext for Sqlite, and when adding a migration I get the following error:

    Unable to create an object of type 'MySqliteDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

    Here is my Datacontext:

    using System;
    using Domain;
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    
    namespace Persistence
    {
        public class DataContext : IdentityDbContext<AppUser>
        {
            public DataContext(DbContextOptions options) : base(options)
            {
            }
    
            public DbSet<App> Apps { get; set; }
            protected override void OnModelCreating(ModelBuilder builder)
            {
                base.OnModelCreating(builder);
            }
        }
    }
    

    and here is my new DBContext for Sqlite:

    using Microsoft.EntityFrameworkCore;
    
    namespace Persistence
    {
        public class MySqliteDbContext : DataContext
        {
            public MySqliteDbContext(DbContextOptions options) : base(options)
            {
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseSqlite("Data source=db.db");
        }
    }
    

    and this is how I configure services in startup.cs in my API project:

    if (Configuration["Provider"] == "SQLlite")
                {
                    services.AddDbContext<DataContext>(opt =>
                        opt.UseSqlite(Configuration.GetConnectionString("SQLlite")));
                }
    else if (Configuration["Provider"] == "MSSQL")
                {
                    services.AddDbContext<DataContext>(opt =>
                        opt.UseSqlServer(Configuration.GetConnectionString("SQLServerConnection")));
                }
     else
                { throw new ArgumentException("Not a valid database type"); }
    

    Below is appsettings.json in my starter API project:

    {
      "Provider": "SQLlite",
      "ConnectionStrings": {
        "SQLlite": "Data source=db.db",
        "SQLServerConnection": "Server=(localdb)\\mssqllocaldb;Database=db;Trusted_Connection=True;MultipleActiveResultSets=true",
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"
    }
    
    

    I add migration by running the following comand:
    dotnet ef migrations add InitialCreate --context MySqliteDbContext --output-dir Migrations/SqliteMigrations -p Persistence -s API

    What might be the issue here?
    Any help is highly appreciated

    Kind Regards,
    Hani

    Sunday, July 19, 2020 7:13 AM

Answers

  • User78124615 posted

    Hello Yijing Sun,

    I explored this link, but the error still appears. I found the solution in this link, and it worked out finally.

    You valuable help is highly appreciated,

    Hani

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 22, 2020 9:38 AM

All replies

  • User475983607 posted

    I don't get the design and I don't think it will work well.  Anyway, your MySqlContext is hardcoded.

    namespace Persistence
    {
        public class MySqliteDbContext : DataContext
        {
            public MySqliteDbContext(DbContextOptions options) : base(options)
            {
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseSqlite("Data source=db.db");
        }
    }

    Sunday, July 19, 2020 12:08 PM
  • User78124615 posted

    May you please elaborate more what should I do to clarify the issue?

    Kind Regards,

    Monday, July 20, 2020 7:24 AM
  • User1535942433 posted

    Hi hanidraidi,

    Accroding to your description,as far as I think,you could do this:

    1.Create a constructor argument.

    2. Initialize DbContextOptions within the context and override the OnConfiguring method and call the methods on the provided DbContextOptionsBuilderConstructor argument.

    3.Use DbContext with dependency injection.

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

    Best regards,

    Yijing Sun

    Tuesday, July 21, 2020 8:43 AM
  • User78124615 posted

    Hello Yijing Sun,

    I explored this link, but the error still appears. I found the solution in this link, and it worked out finally.

    You valuable help is highly appreciated,

    Hani

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 22, 2020 9:38 AM