Created
February 4, 2019 23:03
-
-
Save evgomes/45ccfd24c90c4431f55ca648def25aa2 to your computer and use it in GitHub Desktop.
AppDbContext with products, from Supermarket API
This file contains 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
protected override void OnModelCreating(ModelBuilder builder) | |
{ | |
base.OnModelCreating(builder); | |
builder.Entity<Category>().ToTable("Categories"); | |
builder.Entity<Category>().HasKey(p => p.Id); | |
builder.Entity<Category>().Property(p => p.Id).IsRequired().ValueGeneratedOnAdd().HasValueGenerator<InMemoryIntegerValueGenerator<int>>(); | |
builder.Entity<Category>().Property(p => p.Name).IsRequired().HasMaxLength(30); | |
builder.Entity<Category>().HasMany(p => p.Products).WithOne(p => p.Category).HasForeignKey(p => p.CategoryId); | |
builder.Entity<Category>().HasData | |
( | |
new Category { Id = 100, Name = "Fruits and Vegetables" }, // Id set manually due to in-memory provider | |
new Category { Id = 101, Name = "Dairy" } | |
); | |
builder.Entity<Product>().ToTable("Products"); | |
builder.Entity<Product>().HasKey(p => p.Id); | |
builder.Entity<Product>().Property(p => p.Id).IsRequired().ValueGeneratedOnAdd(); | |
builder.Entity<Product>().Property(p => p.Name).IsRequired().HasMaxLength(50); | |
builder.Entity<Product>().Property(p => p.QuantityInPackage).IsRequired(); | |
builder.Entity<Product>().Property(p => p.UnitOfMeasurement).IsRequired(); | |
builder.Entity<Product>().HasData | |
( | |
new Product | |
{ | |
Id = 100, | |
Name = "Apple", | |
QuantityInPackage = 1, | |
UnitOfMeasurement = EUnitOfMeasurement.Unity, | |
CategoryId = 100 | |
}, | |
new Product | |
{ | |
Id = 101, | |
Name = "Milk", | |
QuantityInPackage = 2, | |
UnitOfMeasurement = EUnitOfMeasurement.Liter, | |
CategoryId = 101, | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment