This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| nebolcontroller_netcore_mssql_adjustments [2021/07/31 11:05] – created nebol | nebolcontroller_netcore_mssql_adjustments [2021/08/02 03:07] (current) – nebol | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== WORK IN PROGRESS ==== | ||
| DAL project: | DAL project: | ||
| - | Install-Package Microsoft.EntityFrameworkCore.SqlServer | + | < |
| Install-Package Microsoft.EntityFrameworkCore.Design | Install-Package Microsoft.EntityFrameworkCore.Design | ||
| + | Install-Package Microsoft.EntityFrameworkCore.SqlServer # if needed for any MSSQL stuff in your DbContext | ||
| + | </ | ||
| Add Models | Add Models | ||
| - | |||
| Add a DbContext | Add a DbContext | ||
| - | public class MixerDbContext | + | < |
| + | public class MyDbContext | ||
| + | { | ||
| + | public MyDbContext() | ||
| { | { | ||
| - | public MixerDbContext() | + | } |
| - | { | + | |
| - | } | + | |
| - | public | + | public |
| - | { | + | { |
| - | } | + | } |
| - | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | + | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| - | { | + | { |
| - | optionsBuilder.UseSqlServer(@" | + | optionsBuilder.UseSqlServer(Config.ConnectionString); |
| - | } | + | } |
| - | public static | + | public static |
| + | { | ||
| + | var optionsBuilder = new DbContextOptionsBuilder< | ||
| + | optionsBuilder.UseSqlServer(Config.ConnectionString); | ||
| + | return (new MyDbContext(optionsBuilder.Options)); | ||
| + | } | ||
| + | |||
| + | protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| + | { | ||
| + | modelBuilder.RemovePluralizingTableNameConvention(); | ||
| + | |||
| + | base.OnModelCreating(modelBuilder); | ||
| + | } | ||
| + | |||
| + | public DbSet< | ||
| + | public DbSet< | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Here's a RemovePluralizingTableNameConvention() that works for .NET Core: | ||
| + | |||
| + | < | ||
| + | public static class ModelBuilderExtensions | ||
| + | { | ||
| + | public static void RemovePluralizingTableNameConvention(this ModelBuilder modelBuilder) | ||
| + | { | ||
| + | foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes()) | ||
| { | { | ||
| - | var optionsBuilder = new DbContextOptionsBuilder< | + | #if (NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2) |
| - | optionsBuilder.UseSqlServer(Config.ConnectionString); | + | entity.Relational().TableName = entity.DisplayName(); |
| - | return (new MixerDbContext(optionsBuilder.Options)); | + | #else // for 3.0 and onwards |
| + | entity.SetTableName(entity.DisplayName()); | ||
| + | #endif | ||
| } | } | ||
| - | protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| - | { | ||
| - | modelBuilder.RemovePluralizingTableNameConvention(); | ||
| - | |||
| - | base.OnModelCreating(modelBuilder); | ||
| - | } | ||
| - | public DbSet< | ||
| - | public DbSet< | ||
| } | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Update Entity Framework Tools if needed === | ||
| + | PM> dotnet tool update --global dotnet-ef | ||
| + | |||
| + | |||
| + | === Create a migration === | ||
| + | < | ||
| + | dotnet ef migrations add InitialCreate --project MyProject.DAL | ||
| + | </ | ||
| + | |||
| + | === Update the database === | ||
| + | < | ||
| + | dotnet ef database update --project Mixer.DAL | ||
| + | </ | ||
| Main project: | Main project: | ||