Inheritance in the Entity Framework

0

I'm now reading about cross-domain inheritance in EF. I came across this example:

public abstract class BillingDetail
{
    public int BillingDetailId { get; set; }
    public string Owner { get; set; }
    public string Number { get; set; }
}

[Table("BankAccounts")]
public class BankAccount : BillingDetail
{
    public string BankName { get; set; }
    public string Swift { get; set; }
}

[Table("CreditCards")]
public class CreditCard : BillingDetail
{
    public int CardType { get; set; }
    public string ExpiryMonth { get; set; }
    public string ExpiryYear { get; set; }
}

public class InheritanceMappingContext : DbContext
{
    public DbSet<BillingDetail> BillingDetails { get; set; }
}

I noticed that only the derived classes get the annotation [Table("NameTable")] . Assuming then that Owner property must be mandatory in derived classes, ( [Required] ), and that it must also have its character size limited to 50, ( [MaxLenght(50)] ). In these cases the annotation is in the parent class, correct?

    
asked by anonymous 28.05.2016 / 17:54

2 answers

1

Correct. Assuming you want to have a table for each type. It could not be any different, since the properties do not appear in the child classes.

Of course the rest of the modeling needs to be right too:)

    
28.05.2016 / 18:32
0

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; }
}
    
28.05.2016 / 21:15