Giter Site home page Giter Site logo

mysql-dotnet-core's Introduction

Convert an ASP.NET Core project to use MySQL with Entity Framework.

Install NuGet packages

Install the following NuGet package in the ASP.NET web application project:

MySql.Data.EntityFrameworkCore

Update appsettings.json

Configure connection string in project's appsettings.json:

"ConnectionStrings":{
    "DefaultConnection":"server=localhost;userid=myusername;password=mypassword;database=mydatabase;"
},

Modify Startup.cs

Add using statements to Startup.cs source code:

using MySQL.Data.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;

Then in the same file's ConfigureServices() method, replace the UseSqlite option with MySQL:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
            options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));

Create Entity Framework Migration Table in MySQL

Running the dotnet ef fails initially as the __efmigrationshistory table doesn't exist. Until this is resolved by the Entity Framework migration tools, manually create the migrations history table in the MySQL database by executing the following SQL script.

use mydatabase;

CREATE TABLE `mydatabase`.`__EFMigrationsHistory` (
  `MigrationId` text NOT NULL,
  `ProductVersion` text NOT NULL,
  PRIMARY KEY (`MigrationId`(255)));

Run Entity Framework Migrations

Execute the following comment inside the project directory, where the project.json file is located:

$ dotnet ef database update

After running the migration, the database is created and web application is ready to be run.

mysql-dotnet-core's People

Contributors

jasonsturges avatar

Watchers

Xavier Peña avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.