Dmitry Sazonov
Dmitry Sazonov

Reputation: 115

How to Exclude from migration Auto-Generated table in EF?

i have table Asp_net_users which is many-to-many relation with table Clients. There are no mapped object in between them, it been auto -generated by EF:

modelBuilder.Entity<Client>(entity =>
{
    entity.Property(e => e.State)
        .HasConversion<string>();

    entity.HasMany(d => d.Users)
        .WithMany(p => p.Clients)
        .UsingEntity<Dictionary<string, object>>(
            "ClientUser",
            l => l.HasOne<AspNetUser>().WithMany().HasForeignKey("UserId"),
            r => r.HasOne<Client>().WithMany().HasForeignKey("ClientId"),
            j =>
            {
                j.HasKey("ClientId", "UserId");
                j.ToTable("client_users"); // ,"identity"
                j.IndexerProperty<string>("ClientId");
                j.IndexerProperty<string>("UserId");
            });
});

The code above generating table client_users which i don't want in my migration.

I've tried to add to code above:

.Metadata
.SetIsTableExcludedFromMigrations(true)

no luck.

I know I can exclude using this method

modelBuilder.Entity<AspNetUser>().ToTable("asp_net_users", "identity", t => t.ExcludeFromMigrations());

but the problem is: above mentioned table is auto generated and I don't know how to do ToTable on it.

tried

modelBuilder.Entity().ToTable("client_users", t => t.ExcludeFromMigrations());

but it don't compile. modelBuilder.Entity() wants to have an object.

Please help

Upvotes: 2

Views: 659

Answers (1)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 115

I added j.Metadata.SetIsTableExcludedFromMigrations(true); inside and it worked:

entity.HasMany(d => d.Users)
  .WithMany(p => p.Clients)
  ...
  j =>
  {
      ...
      j.Metadata.SetIsTableExcludedFromMigrations(true);
  });

Upvotes: 2

Related Questions