Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Two migration sets with two DBContext generate an error

$
0
0

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


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>