0

I have the following legacy table structure (simplified for this post) enter image description here

The following is my feeble attempt at configuring the Entity:

public class EntityConfiguration : EntityTypeConfiguration<Entity> {
public EntityConfiguration() {
  ToTable("Entity");
  HasKey(x => x.Id);
  Property(x => x.Id)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

  HasMany(x => x.TypeOneUpdateBlacklist)
    .WithMany()
    .Map(x => {
      x.ToTable("UpdateBlacklist");
      x.MapLeftKey("EntityId");
      x.MapRightKey("UpdateId");
    });

  HasMany(x => x.TypeTwoUpdateBlacklist)
    .WithMany()
    .Map(x => {
      x.ToTable("UpdateBlacklist");
      x.MapLeftKey("EntityId");
      x.MapRightKey("UpdateId");
    });
}

The configuration renders this error:

The EntitySet 'EntityBlacklistUpdate' with schema 'dbo' and table 'UpdateBlacklist' was already defined. Each EntitySet must refer to a unique schema and table.

Is there away to configure this? Thanks in advance

Dan
  • 21
  • 2
  • It would appear this is not possible. The only workable solution I can come up with is two different tables TypeOneUpdateBlacklist and TypeTwoUpdateBlacklist. – Dan Apr 19 '13 at 15:46

1 Answers1

0

You should be able to create the many-to-many mapping with the base type Update:

public class EntityConfiguration : EntityTypeConfiguration<Entity> {
    public EntityConfiguration() {
    ToTable("Entity");
    HasKey(x => x.Id);
    Property(x => x.Id)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

    HasMany(x => x.Updates)
        .WithMany()
        .Map(x => {
            x.ToTable("UpdateBlacklist");
            x.MapLeftKey("EntityId");
            x.MapRightKey("UpdateId");
    });
}

However, it would require that your class Entity does only have a navigation collection Updates of the base type instead of two navigation collections for the two derived types. And it is only possible if the database schema really represents an inheritance model, i.e. a given Update row can either have a related TypeOneUpdate or a TypeTwoUpdate row, both never both. If it can have both, you cannot map this with TPT but you would have to create one-to-one relationships.

Slauma
  • 175,098
  • 59
  • 401
  • 420