Using the configuration of the Entity Framework Fluent a> this is very clear, you should put these detailed settings in the configuration file inheriting from the EntityTypeConfiguration .
Please note:
public class BankAccountConfiguration: EntityTypeConfiguration<BankAccount>
{
public BankAccountConfiguration()
{
ToTable("BankAccounts");
Property(c => c.BankName)
.IsRequired();
Property(c => c.Swift)
.IsRequired();
}
}
public class CreditCardConfiguration : EntityTypeConfiguration<CreditCard>
{
public CreditCardConfiguration()
{
ToTable("CreditCards");
Property(c => c.CardType)
.IsRequired();
Property(c => c.ExpiryMonth)
.IsRequired();
Property(c => c.ExpiryYear)
.IsRequired();
}
}
public class BillingDetailConfiguration : EntityTypeConfiguration<BillingDetail>
{
public BillingDetailConfiguration()
{
ToTable("BillingDetail");
HasKey(c => c.BillingDetailId);
Property(c => c.BillingDetailId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.IsRequired();
Property(c => c.Number)
.IsRequired();
Property(c => c.Owner)
.IsRequired()
.HasMaxLength(50);
}
}
Configuring the DbContext class
public class InheritanceMappingContext : DbContext
{
public InheritanceMappingContext()
:base("Conn1")
{
}
public DbSet<BillingDetail> BillingDetails { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new BillingDetailConfiguration());
modelBuilder.Configurations.Add(new BankAccountConfiguration());
modelBuilder.Configurations.Add(new CreditCardConfiguration());
}
}
Recording Information:
InheritanceMappingContext c = new InheritanceMappingContext();
BankAccount b = new BankAccount();
b.BankName = "N";
b.Number = "1";
b.Owner = "O";
b.Swift = "O";
c.BillingDetails.Add(b);
c.SaveChanges();
In this specific case a table was created for each configuration class and the rules placed in each configuration class, this improves code reading, but nothing prevents the use of annotations as well
in>
I believe that the inheritance is very clear and your question statement is correct, where the settings are each in its place of origin.
Using DataAnnotations
[Table("BillingDetail")]
public abstract class BillingDetail
{
[Key()]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BillingDetailId { get; set; }
[Required()]
[MaxLength(50)]
public string Owner { get; set; }
[Required()]
public string Number { get; set; }
}
[Table("BankAccounts")]
public class BankAccount : BillingDetail
{
[Required()]
public string BankName { get; set; }
[Required()]
public string Swift { get; set; }
}
[Table("CreditCards")]
public class CreditCard : BillingDetail
{
[Required()]
public int CardType { get; set; }
[Required()]
public string ExpiryMonth { get; set; }
[Required()]
public string ExpiryYear { get; set; }
}