Using the configuration of Entity Framework Fluent this is very clear, you should put these detailed settings in the inheriting settings file from the class Entitytypeconfiguration.
Observe:
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 the code reading, but, nothing prevents to use the annotations also
I believe that so the inheritance is very clear and your statement question is correct, where the settings are each in their 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; }
}
But still the parent class may be without the note
[Table]
? That is, only with the annotations in the properties.– Matheus Saraiva
Yes, you do not need the note, a table will be created for it. There is an example in TPT (which seems to be what you want) to see how it is: https://datatellblog.wordpress.com/2015/04/12/inheritance-modelling-patterns-with-entity-framework-6-code-first/
– Maniero