Apply a property to discriminate an inheritance in the Entity Framework

0

Since I can choose a field as TipoOperacionId to be a discriminator, I have the following code.

public MovimientoMap()
    {
        ToTable("Movimientos");
        HasKey(c => c.MovimientoId);
        Property(c => c.MovimientoId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(c => c.MovimientoId).HasColumnOrder(0);

        HasRequired(c=>c.TipoOperacion).WithMany(c=>c.Movimientos).HasForeignKey(c=>c.TipoOperacionId)
            .WillCascadeOnDelete(false);
        Property(c => c.TipoOperacionId).HasColumnOrder(1);
        Property(c => c.FechaMovimiento).HasColumnOrder(2);

        Map<MovimientoVenta>(c => c.Requires("Type")
            .HasValue(1));

        Map<MovimientoCompra>(c => c.Requires("Type")
            .HasValue(2));
    }

Data Dase

    
asked by Pedro Ávila 25.08.2016 в 23:58
source

1 answer

0

Is that you do not need to hide anything, the property TipooperacionId in the class Movimientos is over, you should remove it

When you map only indicate the name of the field in the table that acts as a discriminator, you do not need ownership in the class.

Re-check the article where I explain on the subject

[Entity Framework] [Code First] Inheritance - Table by hierarchy - Table per Hierarchy (TPH)

You'll see that at no time do I define the property Type in the classes, but if the field is in the table

    
answered by 26.08.2016 в 01:23