简体   繁体   中英

ef core code first generic inheritance mapping

All Positions have a Location (Many-to-1). Different Location types have different Position types

Model:

public abstract class Location
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int AreaId { get; set; }
    public Area Area { get; set; }
    public byte[] ConcurrencyToken { get; set; }
}
public abstract class Location<T> : Location where T : Position
{
    public ICollection<T> Positions { get; set; } = new List<T>();
}

public class Bay : Location<BayRow> {}
public class StandardLocation : Location<Position> {}

public class Position
{
    public int Id { get; set; }
    public int? Place { get; set; }
    public int LocationId { get; set; }
    public Location Location { get; set; }
    public byte[] ConcurrencyToken { get; set; }
}
public class BayRow : Position
{
    public int? Row { get; set; }
}

The above is abbreviated, there are many more implementations of each. All locations extend the generic class.

Mapping:

modelBuilder.Entity<Position>(entity =>
{                
    entity.ToTable("Position")
          .HasDiscriminator<int>("Type")
          .HasValue<Position>(1)
          .HasValue<BayRow>(2);
    entity.Property(x => x.ConcurrencyToken).IsConcurrencyToken();
    //THIS IS THE ISSUE*
    entity.HasOne(x => x.Location as Location<Position>).WithMany(x => x.Positions).HasForeignKey(x => x.LocationId);
});

modelBuilder.Entity<Location>(entity =>
{
    entity.HasIndex(x => new {x.Name, x.AreaId}).IsUnique(true);
    entity.Property(x => x.ConcurrencyToken).IsConcurrencyToken();
    entity.HasDiscriminator<int>("Type")
          .HasValue<StandardLocation>(1)
          .HasValue<Bay<BayRow>>(2)
});

modelBuilder.Entity<Bay<BayRow>>(entity =>
{
    entity.HasMany(x => x.Positions).WithOne(x => x.Location as Bay<BayRow>)
                .HasForeignKey(x => x.LocationId).OnDelete(DeleteBehavior.Cascade);
});

modelBuilder.Entity<BayRow>(entity =>
{
    entity.Property(x => x.Row).HasColumnName("Row");
});

*The non-generic Location does not have positions

I've tried adding the collection to the base Location purely for mapping, to avoid ef duplicating/aliasing each location Impl ie BayId as LocationId.

publiic ICollection<Position> Positions { get; set; }

and with the new keyword to hide the base collection, but ef projects 2 collections...

public new ICollection<T> Positions { get; set; }

Any insight would be much appreciated.

I'm not sure this is supported by Entity Framework, without generating two tables - one for Bay and one for StandardLocation.

You might try this as a workaround.

    public interface ITypedPosition<T> where T: Position
    {
        IEnumerable<T> Positions { get; }
    }

    public abstract class Location
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int AreaId { get; set; }
        public Area Area { get; set; }
        public byte[] ConcurrencyToken { get; set; }
        public ICollection<Position> Positions { get; set; }
    }

    public class Bay : Location, ITypedPosition<BayRow>
    {
        IEnumerable<BayRow> ITypedPosition<BayRow>.Positions => base.Positions.OfType<BayRow>();
    }
    public class StandardLocation : Location, ITypedPosition<Position>
    {
        IEnumerable<Position> ITypedPosition<Position>.Positions => base.Positions.OfType<Position>();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM