feat(ApiDbContext): Mit Seed hinzugefügt
This commit is contained in:
parent
6ba7fe230a
commit
67a4e91a1b
11
src/DigitalData.Swagger.MockAPI/Data/ApiContext.cs
Normal file
11
src/DigitalData.Swagger.MockAPI/Data/ApiContext.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace DigitalData.Swagger.MockAPI.Data;
|
||||||
|
|
||||||
|
public class ApiContext(DbContextOptions<ApiContext> options) : DbContext(options)
|
||||||
|
{
|
||||||
|
public DbSet<Company> Companies { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Product> Products { get; set; }
|
||||||
|
}
|
||||||
75
src/DigitalData.Swagger.MockAPI/Data/DBSeeder.cs
Normal file
75
src/DigitalData.Swagger.MockAPI/Data/DBSeeder.cs
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DigitalData.Swagger.MockAPI.Data
|
||||||
|
{
|
||||||
|
public class DBSeeder
|
||||||
|
{
|
||||||
|
public static void AddCompaniesData(WebApplication app)
|
||||||
|
{
|
||||||
|
var scope = app.Services.CreateScope();
|
||||||
|
var db = scope.ServiceProvider.GetService<ApiContext>();
|
||||||
|
|
||||||
|
db.Companies.AddRange(new List<Company>()
|
||||||
|
{
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
ID = 1,
|
||||||
|
Name = "TechCorp",
|
||||||
|
Size = 500,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new() { ID = 1, CompanyID = 1, Name = "Laptop X1", Price = 1200.99m },
|
||||||
|
new() { ID = 2, CompanyID = 1, Name = "Smartphone S2", Price = 799.49m }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Company
|
||||||
|
{
|
||||||
|
ID = 2,
|
||||||
|
Name = "AutoWorks",
|
||||||
|
Size = 300,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new Product { ID = 3, CompanyID = 2, Name = "Electric Car E1", Price = 35000m },
|
||||||
|
new() { ID = 4, CompanyID = 2, Name = "Hybrid SUV H2", Price = 42000m }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Company
|
||||||
|
{
|
||||||
|
ID = 3,
|
||||||
|
Name = "MediHealth",
|
||||||
|
Size = 200,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new() { ID = 5, CompanyID = 3, Name = "Vitamin Pack V10", Price = 19.99m },
|
||||||
|
new() { ID = 6, CompanyID = 3, Name = "Protein Shake P5", Price = 25.49m }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Company
|
||||||
|
{
|
||||||
|
ID = 4,
|
||||||
|
Name = "HomeStyle",
|
||||||
|
Size = 150,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new() { ID = 7, CompanyID = 4, Name = "Smart Blender B3", Price = 89.99m },
|
||||||
|
new() { ID = 8, CompanyID = 4, Name = "Air Purifier A1", Price = 149.99m }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Company
|
||||||
|
{
|
||||||
|
ID = 5,
|
||||||
|
Name = "GadgetZone",
|
||||||
|
Size = 400,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new() { ID = 9, CompanyID = 5, Name = "Wireless Earbuds W1", Price = 99.99m },
|
||||||
|
new() { ID = 10, CompanyID = 5, Name = "Smart Watch G5", Price = 199.99m }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
db.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.1.3" />
|
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.1.3" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user