GithubHelp home page GithubHelp logo

dotnetwithmysqlapi's Introduction

ASP.NET 6 Web API With And MySQL

A brief description of how to create a .net web api project with mysql and necessary dependency.

Project Preparation

  1. Create a ASP.NET Core Web API project on .NET 6.0
  2. Install these NuGet packages.
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Tools
  • MySql.EntityFrameworkCore
  1. Scaffold-DbContext from the existing MySql database.
Scaffold-DbContext "server=servername;port=portnumber;user=username;password=pass;database=databasename" MySql.EntityFrameworkCore -OutputDir Entities -f
  1. If MySql.EntityFrameworkCore throws and error while scaffold then most probably it's for NuGet packages version. Downgrade all three packages into version 5.0.16 as mentioned in this documentation.After successful scaffolding upgrade these packages back to the their latest versions.

  2. After successfully scaffolding we'll find connection string into newly created DbContext file.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
  if (!optionsBuilder.IsConfigured)
  { optionsBuilder.UseMySQL("server=localhost;port=3306;user=root;password=;database=database");
  }
}
  1. But it's not appropriate that the connection string to the database is specified in the OnConfiguring method. So we'll move connection string into appsettings.json file.
"ConnectionStrings": {
  "DefaultConnection": "server=localhost;port=3306;user=root;password=;database=demo;"
}
  1. Then, in the Program.cs, we'll add as a service to refer DBContext, and that must have the reference of DefaultConnection specified in the appsettings.json file.
using Microsoft.EntityFrameworkCore;
using dotNetWithMySqlAPI.Entities;
using MySql.EntityFrameworkCore.Extensions;
builder.Services.AddEntityFrameworkMySQL().AddDbContext<dotnetapiContext>(options => {
    options.UseMySQL(builder.Configuration.GetConnectionString("DefaultConnection"));
});
  1. Now add a api controller using the model and run the application.

Acknowledgements

dotnetwithmysqlapi's People

Watchers

Imamul Islam nAyeem 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.