This article explains how to connect MySQL using Entity Framework Core.
This one require Visual Studio 2017, Dot net Core 2.0 and above and MySQL Version 8.0.12 and above.
I know you have a question how to find MySQL version and the answer is below.
This one require Visual Studio 2017, Dot net Core 2.0 and above and MySQL Version 8.0.12 and above.
I know you have a question how to find MySQL version and the answer is below.
- Open MySQL Workbench
- Run this query select @@version
Okay Lets Start.
1. Select File -->New --> Project in the Visual Studio
2. Select 'Console Application (.NET Core)' under '.Net Core' in the New Project Window
3. Solution explore will looks like below
4. Add New Project
5. Select 'Class Library(.Net Core);
6. Open NuGet Packet Manager for Data Layer project.
7. It is displaying all installed packages
8. Install below packages
- MySql.Data.EntityFrameworkCore --> Version 6.10.9
- MySql.Data.EntityFrameworkCore.Design --> Version 6.10.9
- Microsoft.EntityFrameworkCroe.Tools --> version 2.0.3
Installed Packages.
9. Create new database in MySQL. https://www.mysqltutorial.org/mysql-sample-database.aspx/ use this if you really need.
10. Scaffold DB models using Package Manager Console.
- Open Package Manage Console and Select Default Project to your EF Core Project
- Run below command with your own values.
Scaffold result folders as below
11. Add DataLayer as Dependency of "MyApplication"
12. Use below code in the Program.cs
using System;
using DataLayer.Models.ClassicModels;
using System.Linq;
using System.Collections.Generic;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
using (classicmodelsContext context = new classicmodelsContext())
{
List<string> cityCollection = context.Offices.Select(office => office.City).ToList();
Console.WriteLine("Office list as below");
foreach(string city in cityCollection)
{
Console.WriteLine(city);
}
}
Console.ReadKey();
}
}
}
13. Output as below
Happy Coding :)
No comments:
Post a Comment