In this article I’ll show step by step instructions of correct use Entity Framework Core in your ASP.NET Core project with MySql Database and traditional First of all you have to know some basic questions.I discussed them below. ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern cloud-based, Internet-connected applications. With ASP.NET Core, you can: Build web apps and services, IoT apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux. A Web API is an application programming interface for either a web server or a web browser. It is a web development concept, usually limited to a web application's client-side (including any web frameworks being used), and thus usually does not include web server or browser implementation details such as SAPIs or APIs unless publicly accessible by a remote web application Entity Framework is an Object
Relational Mapper (ORM) which is a type of tool that simplifies mapping between objects in your software to the tables and columns of a relational database. Object-relational mapping in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. MySQL is an open source relational database.MySQL is cross platform which means it runs on a number of different platforms such as Windows, Linux, and Mac OS etc. This guide explains setting up a production-ready ASP.NET Core Web API using Entity Framework with MySQL Database. Our Web API can perform basic CRUD operations. Following the steps to install MySQL in your windows 10 The general MySQL Installer download is available at this link.MySQL Workbench can be installed using the Windows MSI Installer package. The MSI package bears the name The MySQL Installer application can install, upgrade, and manage most MySQL products, including MySQL Workbench. Next-->
You are set... Step 2: Create a Database using MySQL WorkbenchYou can use the MySQL Workbench GUI to create a database. You can also create a database programmatically but here's how to do it via the GUI. In the following example, we create a new database called "StudentDB".
Step 3: Create a ASP.NET Core Web API
N.B.
Unselect the configure for Https Press Step 4: Adding Dependencies in ASP.NET CoreBefore we start our project need a few dependencies. We will add them all by NuGet Package Manager. The list of packages is below:
I use Polemo provider instead Oracle provider (MySql.Data.EntityFrameworkCore) because Oracle’s connector doesn’t support EF migrations, also relationships there can be implemented only in Fluent.API. EF Tools I installed just for simplifying entering commands in Package Manager Console (Final Step of this story). Step 5: Adding Models and Database ContextFor start we need basic EF Models and DbContext. In this example I will create simple models with user and his pets. This example also will demonstrate auto-creation of relationships by Entity Framework. Firstly create a folder named
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace CRUDWebAPIWithMySQL.Models { public class StudentDetail { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } [Required] [Column(TypeName = "nvarchar(100)")] public string Name { get; set; } [Required] [Column(TypeName = "varchar(11)")] public string Phone_Number { get; set; } [Required] [Column(TypeName = "varchar(10)")] public string Birth_Date { get; set; } } } Then Create DBContext file named using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace CRUDWebAPIWithMySQL.Models { public class StudentDetailContext : DbContext { public StudentDetailContext(DbContextOptions<StudentDetailContext> options) : base(options) { } public DbSet<StudentDetail> StudentDetails { get; set; } //Table Name StudentDetails } } Step 6 : Configure Dependency InjectionIn this example I will save our connection string in "ConnectionStrings": { "DBConn": "Server=<Your IP>;port=3306;Database=studentdb;User Id=root;Password=1234;" } Here is our's. We get our mySQL Server IP and port number from "ConnectionStrings": { "DBConn": "Server=localhost;port=3306;Database=studentdb;User Id=root;Password=1234;" } Now we must notify about existing our database context in using Pomelo.EntityFrameworkCore.MySql.Infrastructure; using CRUDWebAPIWithMySQL.Models; using Microsoft.EntityFrameworkCore; Then adding public void ConfigureServices(IServiceCollection services) { services.AddDbContextPool<Your DBContext Class name>( options => options.UseMySql(Configuration.GetConnectionString(<Connection String Name>) )); services.AddMvc(); } public void ConfigureServices(IServiceCollection services) { services.AddDbContextPool<StudentDetailContext>( options => options.UseMySql(Configuration.GetConnectionString("DBConn") )); services.AddControllers(); } Then create
Step 7 : MigrationsIf you installed EF Tools as me, than you can just run next 2 commands in Package Manager Console: open your Add-Migration <MigrationName> Update-Database Here is mine, Add-Migration InitialCreate Update-Database If any problem in your Migration delete your previous migrations. After a successful
migration , a table named Step 8 : Final CheckingWe are now all set. I added some rows myself. Open
Now go to controller class named Here Our sub domain is Now
Here is an example.This API works for getting details of all student,
Or For adding row ,we can How do I connect MySQL database to web core API?How to Connect to MySQL from .. Install MySqlConnector. First, install the MySqlConnector NuGet package. ... . Connection String. A typical connection string for MySQL is: Server=YOURSERVER;User ID=YOURUSERID;Password=YOURPASSWORD;Database=YOURDATABASE. ... . Configure Service (ASP.NET Core) ... . Open and Use the Connection.. Can we connect ASP.NET with MySQL?You will need to download and install the MySQLConnector in order to connect to the MySQL database in ASP.Net. After installation is complete you need to open Windows Explorer and look for the MySql installation in the Program Files folder of your Windows drive.
Can I use C# with MySQL database?Before you start to connect your application to MySql, you need to add add the mysql Reference in your project. To do so, right click our project name, and choose Add Reference, then choose "MySql. Data" from the list. Next, you need to add MySql Library in your C# project.
How do I add a database to Web API?These are the steps we need to follow:. Create a Blank Web API project.. Select Web API in the template selection window.. Create a table and Insert data to the database table.. Add a web service.. Add an ADO.NET Entity Data Model.. Create a new connection with SQL server.. Generate entities from Database.. Add a Controller.. |