Reputation: 10078
I have ASP.Net Core
command-line application where I try to copy data from one database from another. So ideally, I would want to have something like this
services.AddDbContext<IdentityDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FromConnection")));
services.AddDbContext<IdentityDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ToConnection")));
Obviously, this won't work since the type in the generic is the same. If I could do something like this:
var fromContext = new IdentityDbContext<IdentityUser>();
var toContext = new IdentityDbContext<IdentityUser>();
services.AddDbContext(fromContext, options => options.UseSqlServer(Configuration.GetConnectionString("FromConnection")));
services.AddDbContext(toContext, options => options.UseSqlServer(Configuration.GetConnectionString("ToConnection")));
It's a small utility program; so I can compromise (somewhat) on the dependency injection - is there any way to accomplish this?
In this case I am trying to copy roles and claims from Identity tables; so the same problem is with IdentityRole
and IdentityRoleClaim
classes - but the first step is to set up DbContext
separately.
UPDATE: If I were to use derived classes from DbContext
like this:
public class IdentityFromDbContext : IdentityDbContext<IdentityUser> { ... }
public class IdentityToDbContext : IdentityDbContext<IdentityUser> { ... }
I then need to have the following:
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityDbContext>()
And if I were to derive IdentityFromUser
etc. I would get a problem that I need to modify the application model itself
Upvotes: 0
Views: 671
Reputation: 162
In this case, I suggest you go through SQL Server Because the Role and other tables have the Identity field
Upvotes: 0
Reputation: 36645
Did you consider using two DbContext to connect two databases instead of copying data to the other database?You could register them in startup.cs and DI them by using constructor.
Reference:https://stackoverflow.com/a/58022297/11398810
And you could use command add-migration init -context Context1
to specify the migration and update-database -context Context1
to update the database in pmc.
Upvotes: 0
Reputation: 1684
Have you considered having separated contexts that inherit from your MyDbContext
?
i.e. OldDbContext : MyDbContext
and NewDbContext : MyDbContext
.
Upvotes: 1