Last active
March 29, 2017 21:31
-
-
Save A-Programmer/5f85e5b6ccb6f28615b6155dea7d0355 to your computer and use it in GitHub Desktop.
How to use MySql database in Dot Net Core Project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0. Create new console app (dotnet new console) | |
1. Create appsettings.js | |
2. Add this codes to file : | |
{ | |
"ConnectionStrings": | |
{ | |
"SampleConnection": "server=localhost;userid=root;pwd=;port=3305;database=KamranDb3;" | |
} | |
} | |
3. Add this packages using Nuget Package Manager (or just copy this to YourProject.csproj file) : | |
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1"/> | |
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1"/> | |
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="7.0.7-m61"/> | |
<PackageReference Include="MySql.Data" Version="7.0.7-m61"/> | |
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1"/> | |
4. run dotnet restore to restore packages | |
5. Create you Database Context file : | |
namespace ConsoleApplication | |
{ | |
using Microsoft.EntityFrameworkCore; | |
/// <summary> | |
/// The entity framework context with a Employees DbSet | |
/// </summary> | |
public class EmployeesContext : DbContext | |
{ | |
public EmployeesContext(DbContextOptions<EmployeesContext> options) | |
: base(options) | |
{ } | |
public DbSet<Employee> Employees { get; set; } | |
} | |
/// <summary> | |
/// Factory class for EmployeesContext | |
/// </summary> | |
public static class EmployeesContextFactory | |
{ | |
public static EmployeesContext Create(string connectionString) | |
{ | |
var optionsBuilder = new DbContextOptionsBuilder<EmployeesContext>(); | |
optionsBuilder.UseMySQL(connectionString); | |
//Ensure database creation | |
var context = new EmployeesContext(optionsBuilder.Options); | |
context.Database.EnsureCreated(); | |
return context; | |
} | |
} | |
/// <summary> | |
/// A basic class for an Employee | |
/// </summary> | |
public class Employee | |
{ | |
public Employee() | |
{ | |
} | |
public int Id { get; set; } | |
[MaxLength(30)] | |
public string Name { get; set; } | |
[MaxLength(50)] | |
public string LastName { get; set; } | |
} | |
} | |
as you can see i have a table called Employees with 3 fields (Id,Name,LastName). | |
6. Open Program.cs file and replace content of it with this codes : | |
using System; | |
using Microsoft.Extensions.Configuration; | |
namespace ConsoleApplication | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | |
var configuration = builder.Build(); | |
string connectionString = configuration.GetConnectionString("SampleConnection"); | |
// Create an employee instance and save the entity to the database | |
var entry = new Employee() { Id = 1, Name = "Kamran", LastName = "Sadin" }; | |
using (var context = EmployeesContextFactory.Create(connectionString)) | |
{ | |
context.Add(entry); | |
context.SaveChanges(); | |
} | |
Console.WriteLine($"Employee was saved in the database with id: {entry.Id}"); | |
} | |
} | |
} | |
Ok, now run this commands : | |
dotnet restore | |
dotnet build | |
dotnet run | |
You should see "Employee was saved in the database with id: 1" on the screen. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment