"ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=database;Uid=user;Pwd=password;" }So it looks like:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=database;Uid=user;Pwd=password;" } }3. Install following packages:
Microsoft.EntityFrameworkCore
MySql.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design 4. Add new class "ModelExample":
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; namespace AspNetCore7WebApiEFMySqlExample; public class ModelExample { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string Name { get; set; } public string PhoneNumber { get; set; } }5. Add new class:
using Microsoft.EntityFrameworkCore; namespace AspNetCore7WebApiEFMySqlExample; public class ModelExampleContext : DbContext { public ModelExampleContext(DbContextOptions<ModelExampleContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<ModelExample>(entity => { entity.HasKey(e => e.Id); entity.Property(e => e.Name).IsRequired(); }); } }6. In Program.cs add:
builder.Services.AddDbContext<ModelExampleContext>(x => x.UseMySQL(builder.Configuration.GetConnectionString("DefaultConnection")));6. In project (not solution) folder execute:
dotnet ef migrations add InitialCreate dotnet ef database updateExample download from here