The Entity Framework Core Fluent API HasValue
method is used to configure discriminator values for entities in an inheritance hierarchy when a table per hierarchy approach is chosen for mapping inheritance to a database.
The HasValue
method is available on the DiscriminatorBuilder
type, which is returned when the HasDiscriminator method is called. It takes a type parameter specifying the type that the value applies to, and an object representing the value to apply to the specified type. In the following example, the discriminator values for the MobileContract
type and the BroadbandContract
type are configured as 1 and 2 respectively.
language-csharp
|
public class SampleContext : DbContext
{
public DbSet<MobileContract> MobileContracts { get; set; }
public DbSet<TvContract> TvContracts { get; set; }
public DbSet<BroadbandContract> BroadbandContracts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Contract>()
.ToTable("Contracts")
.HasDiscriminator<int>("ContractType")
.HasValue<MobileContract>(1)
.HasValue<BroadbandContract>(2);
}
}
public abstract class Contract
{
public int ContractId { get; set; }
public DateTime StartDate { get; set; }
public int Months { get; set;}
public decimal Charge { get; set; }
}
public class MobileContract : Contract
{
public string MobileNumber { get; set; }
}
public class BroadbandContract : Contract
{
public int DownloadSpeed { get; set; }
}
Data Annotations
It is not possible to configure discriminator values with Data Annotation attributes.