1

I have ASP.NET CORE 2 application with MySQL 5.7 and EF Core 2.0 in code-first approach.

I have setup my database in replication for Read and Write. So how can I configure my EF Core for these two connection string for respective operations i.e. Read and Write?

1 Answer 1

2

It is a good choice to use open source DBMS with .Net core first of all, install these two packages in your project:

  1. Mysql.Data
  2. Mysql.Data.EntityFrameworkCore

You can do it simply from tools->Nuget packageManager -> package manager console

    install-package Mysql.Data
    install-package Mysql.Data.EntityFrameworkCore

or install them to your solution(Manage NuGet packages for solution)

After that go to appsettings.json and add the connection string as follow:

    {"ConnectionStrings": {
          "connectionName":"server=127.0.0.1;port=3306;database=mysqlTestDb;username=mysqlUser;password=mysqlPassword"},

Next, add your Context class and go to your startup.cs class for configuring mysql in DI in the ConfigureServices method add the following code

    string connectionString = Configuration.GetConnectionString("mysqlConnectionName");

    services.AddDbContext<DbContextName>(Options => Options.UseMySQL(connectionString));

After following all these steps, add migrations and update your database. I hope it works well for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.