question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multi DbContext MSSQL & MySQL

See original GitHub issue

I try to use 2 db connection 1 to MSSQL (abp-zero [roles,users,etc]) one to business data on MySQL.

I use the sample code from https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextDemo and I create the table and it’s work when I call a method, based on a AbpModule to need data from MySql i catch an excection System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid object name 'mdm.app_fd_tb_company'. in System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__167_0(Task1 result) in System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke() in System.Threading.Tasks.Task.Execute() So the excetion is relate to System.Data.SqlClient and not to MySql. How can tell to ABP that need to use EF for MySql and not to SQL server.

Another operation that I need to do is to set the initializer Database.SetInitializer<mySqlDbContext>(null); If I not set this row code first say me the database are not update

my db context refered to mysql is declared in this way [DbConfigurationType(typeof(MySqlEFConfiguration))] public class mySqlDbContext : AbpDbContext {

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
andmattiacommented, Apr 15, 2016

First of all many thanks to @hikalkan.

In order to use multi connection:

  • remember to remove default contructor public secondDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } in according to https://github.com/aspnetboilerplate/aspnetboilerplate-templates/blob/master/src/AbpCompanyName.AbpProjectName.EntityFramework/EntityFramework/AbpProjectNameDbContext.cs
  • In same case need to set the second dbcontext to null in SetInitializer
  • Database.SetInitializer<secondDbContext>(null);`

And after this modification my solution work wy MSSQL and MySQL.

mat

0reactions
andmattiacommented, Apr 15, 2016

ok I send it full configuration.

So after a lot of investigation i found the problem(probably) but not the solution.

In EF module I set Configuration.DefaultNameOrConnectionString = "mySql"; // "Default"

So the public class EntityFrameworkConventionalRegistrar : IConventionalDependencyRegistrar use connection name to register by IoC.

I try to use ` Database.SetInitializer<mysqlDbContext>(new aa()); } }

public class aa : IDatabaseInitializer<mysqlDbContext>
{

    public void InitializeDatabase(mysqlDbContextcontext)
    {
        context = new mysqlDbContext();

        //var z = context.Database.Connection;


        //var q = DbProviderFactories.GetFactory("MySql.Data.MySqlClient").CreateConnection();

    }

` In Debug I see the the connection is not MySql but MSSQL but the provider is correctly regitered. I have try the custom repository.

I study your multi connection example and I see that is works in same db instance it’s only a separate database.

To solve my issue I think to need extend AbpModule and AbpEF. in EF need to possibile define a local connection string to allow a rigistration for each module with specific connection.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple DBContext at run time with multiple providers
I have a requirement to create a dotnet core project that uses multiple database providers. One uses a SqlServer provider and the other...
Read more >
Entity Framework Core and Multiple Database Providers
Using Entity Framework Core's multiple database providers is an excellent option for folks selling on-premise software where customers demand ...
Read more >
Multiple DB engine connections support (MySQL, MS SQL, ...
Multiple DB engine connections support (MySQL, MS SQL, Postgre SQL). Video versionSee a video tutorial how to connect with multiple databases.
Read more >
Multi dbContext with mixed type (MSSQL & MySql) #10248
We are moving from .Net to .Net Core so now we need to convert a part where we need to call a remote...
Read more >
Using Multiple Databases in ASP.NET Core via Entity ...
In this article, we'll learn how to add multiple databases in the ASP.NET Core project using Entity Framework Core and Repository Pattern.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found