-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathDemoContext.cs
58 lines (48 loc) · 1.75 KB
/
DemoContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using Microsoft.EntityFrameworkCore;
namespace EntityFramework.Exceptions.Tests;
public class DemoContext : DbContext
{
public DemoContext(DbContextOptions options) : base(options)
{
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ProductSale> ProductSales { get; set; }
public DbSet<ProductPriceHistory> ProductPriceHistories { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Product>().HasIndex(u => u.Name).IsUnique();
builder.Entity<Product>().Property(b => b.Name).IsRequired().HasMaxLength(ProductNameMaxLength);
builder.Entity<ProductSale>().Property(b => b.Price).HasColumnType("decimal(5,2)").IsRequired();
builder.Entity<ProductPriceHistory>().Property(b => b.Price).HasColumnType("decimal(5,2)").IsRequired();
builder.Entity<ProductPriceHistory>().Property(p => p.EffectiveDate).IsRequired();
builder.Entity<ProductPriceHistory>().HasOne(p => p.Product).WithMany().OnDelete(DeleteBehavior.NoAction);
}
public const int ProductNameMaxLength = 25;
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ProductSale
{
public int Id { get; set; }
public decimal Price { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
public class ProductPriceHistory
{
public long Id { get; set; }
public decimal Price { get; set; }
public DateTimeOffset EffectiveDate { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Fullname { get; set; }
}