C
C#2y ago
OukiShogun

❔ Tph strategy and fluent api. How to specify that a property of child class is required

Hi, I am trying to set column of my child entity Product required. Product is a child of Owned class. But the constraint in not applyed. I can using SSMS insert without specify a value. Below my code :
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Owned>()
.UseTphMappingStrategy();
modelBuilder.Entity<Owned>()
.HasDiscriminator(o => o.Descriminator)
.HasValue<Product>((int)OwnedDescriminator.Product);
modelBuilder.Entity<Owned>()
.HasKey(ow => ow.Id);
modelBuilder.Entity<Owned>()
.Property(ow => ow.Id).IsRequired();
modelBuilder.Entity<Owned>()
.Property(ow => ow.Name).IsRequired();
modelBuilder.Entity<Product>()
.HasBaseType<Owned>();
modelBuilder.Entity<Product>()
.Property(pr => pr.Sku).IsRequired(); // TODO TEST with true SQL SERVER
modelBuilder.Entity<Product>()
.Property(pr => pr.PriceSell).IsRequired(); // TODO TEST with true SQL SERVER
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Owned>()
.UseTphMappingStrategy();
modelBuilder.Entity<Owned>()
.HasDiscriminator(o => o.Descriminator)
.HasValue<Product>((int)OwnedDescriminator.Product);
modelBuilder.Entity<Owned>()
.HasKey(ow => ow.Id);
modelBuilder.Entity<Owned>()
.Property(ow => ow.Id).IsRequired();
modelBuilder.Entity<Owned>()
.Property(ow => ow.Name).IsRequired();
modelBuilder.Entity<Product>()
.HasBaseType<Owned>();
modelBuilder.Entity<Product>()
.Property(pr => pr.Sku).IsRequired(); // TODO TEST with true SQL SERVER
modelBuilder.Entity<Product>()
.Property(pr => pr.PriceSell).IsRequired(); // TODO TEST with true SQL SERVER
}
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server