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 usingtwo
migration sets, I followed the documentation, and added a new DbContext for Sqlite, and when adding a migration I get the following error:
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
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.
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
None
0 Points
7 Posts
Two migration sets with two DBContext generate an error
Jul 19, 2020 07:13 AM|hanidraidi|LINK
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:
and here is my new DBContext for Sqlite:
and this is how I configure services in startup.cs in my API project:
Below is appsettings.json in my starter API project:
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
All-Star
53691 Points
24031 Posts
Re: Two migration sets with two DBContext generate an error
Jul 19, 2020 12:08 PM|mgebhard|LINK
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"); } }
None
0 Points
7 Posts
Re: Two migration sets with two DBContext generate an error
Jul 20, 2020 07:24 AM|hanidraidi|LINK
May you please elaborate more what should I do to clarify the issue?
Kind Regards,
Contributor
4040 Points
1568 Posts
Re: Two migration sets with two DBContext generate an error
Jul 21, 2020 08:43 AM|yij sun|LINK
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
None
0 Points
7 Posts
Re: Two migration sets with two DBContext generate an error
Jul 22, 2020 09:38 AM|hanidraidi|LINK
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