Run migrations on server startup
This commit is contained in:
parent
8fe3c5fe5d
commit
bd229cddd4
@ -1,10 +1,30 @@
|
||||
using Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
var host = CreateHostBuilder(args).Build();
|
||||
using(var scope = host.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
|
||||
try
|
||||
{
|
||||
var context = services.GetRequiredService<StoreContext>();
|
||||
await context.Database.MigrateAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
logger.LogError(ex, "An error occured during migration");
|
||||
}
|
||||
}
|
||||
|
||||
host.Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
|
20
Infrastructure/Data/Config/ProductConfiguaration.cs
Normal file
20
Infrastructure/Data/Config/ProductConfiguaration.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using Core.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Infrastructure.Data.Config
|
||||
{
|
||||
public class ProductConfiguaration : IEntityTypeConfiguration<Product>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Product> builder)
|
||||
{
|
||||
builder.Property(p => p.Id).IsRequired();
|
||||
builder.Property(p => p.Name).IsRequired().HasMaxLength(100);
|
||||
builder.Property(p => p.Description).IsRequired().HasMaxLength(256);
|
||||
builder.Property(p => p.Price).HasColumnType("decimal(18,2)");
|
||||
builder.Property(p => p.PictureUrl).IsRequired();
|
||||
builder.HasOne(b => b.ProductBrand).WithMany().HasForeignKey(p => p.ProductBrandId);
|
||||
builder.HasOne(t => t.ProductType).WithMany().HasForeignKey(p => p.ProductTypeId);
|
||||
}
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(StoreContext))]
|
||||
[Migration("20220509234932_InitialCreate")]
|
||||
[Migration("20220510050050_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@ -25,16 +25,21 @@ namespace Infrastructure.Data.Migrations
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasMaxLength(256)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("PictureUrl")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<decimal>("Price")
|
||||
.HasColumnType("TEXT");
|
||||
.HasColumnType("decimal(18,2)");
|
||||
|
||||
b.Property<int>("ProductBrandId")
|
||||
.HasColumnType("INTEGER");
|
@ -40,10 +40,10 @@ namespace Infrastructure.Data.Migrations
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
Name = table.Column<string>(type: "TEXT", nullable: true),
|
||||
Description = table.Column<string>(type: "TEXT", nullable: true),
|
||||
Price = table.Column<decimal>(type: "TEXT", nullable: false),
|
||||
PictureUrl = table.Column<string>(type: "TEXT", nullable: true),
|
||||
Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
|
||||
Description = table.Column<string>(type: "TEXT", maxLength: 256, nullable: false),
|
||||
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
|
||||
PictureUrl = table.Column<string>(type: "TEXT", nullable: false),
|
||||
ProductTypeId = table.Column<int>(type: "INTEGER", nullable: false),
|
||||
ProductBrandId = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
},
|
@ -23,16 +23,21 @@ namespace Infrastructure.Data.Migrations
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasMaxLength(256)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("PictureUrl")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<decimal>("Price")
|
||||
.HasColumnType("TEXT");
|
||||
.HasColumnType("decimal(18,2)");
|
||||
|
||||
b.Property<int>("ProductBrandId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System.Reflection;
|
||||
using Core.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@ -11,5 +12,10 @@ namespace Infrastructure.Data
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public DbSet<ProductBrand> ProductBrands { get; set; }
|
||||
public DbSet<ProductType> ProductTypes { get; set; }
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user