bbqchickenrobot
bbqchickenrobot

Reputation: 3709

EF Core Migration Adding Tables from Other Contexts

I am trying to create a 1..1 relationship between two tables (CustomerProfile & ProviderProfile) and a main ASP.NET Core IdentityUser table (AppUser). When I run the migration all tables are being generated from each of the schemas (each of the 3 tables have their own Schema setup) inside of the AppUser Schema (Users). Here is how the IdentityContext is configured:

public class IdentityContext : IdentityDbContext<AppUser, AppRole, string>
{
    private const string schema = "Users";

    
    public DbSet<CustomerProfile> CustomerProfile { get; set; }
    public DbSet<ProviderProfile> ProviderProfile { get; set; }

   public override void OnModelCreating(ModelBuilder builder)
   {
        base.OnModelCreating(builder);
        builder.HasDefaultSchema(schema);

        builder.Entity<AppUser>()
            .HasOne(x => x.ProviderProfile)
            .WithOne(x => x.User)
            .HasForeignKey<ProviderProfile>(x => x.UserId);
        
        builder.Entity<AppUser>()
            .HasOne(x => x.CustomerProfile)
            .WithOne(x => x.User)
            .HasForeignKey<ProviderProfile>(x => x.UserId);
    }
}

Here is the AppUser (IdentityUser) class definition:

public class AppUser : IdentityUser
{
    public bool IsServiceProvider { get; set; }
    public virtual CustomerProfile CustomerProfile { get; set; }
    public virtual ProviderProfile ProviderProfile { get; set; }
}

When I run the migration the database Users schema as a crap ton of tables form the other DbContexts that hold references to CustomerProfile and ProviderProfile in turn which have all those tables as well (where they're supposed to live).

To provide more detail: The CustomerProfile and ProviderProfile both have their own DbContext's and have references to several other tables in them (these related tables belong to the schema's for each profile). The tables in each of the profiles belong to the same schema as the Profile classes. Those Entites/Tables are the culprits that are being generated in the Identity schema (Users) of which they do not belong.

There are 3 schemas:

  1. Users (IdentityContext - Users schema)
  2. Customers (CustomersContext - Customers schema)
  3. Providers (ProvidersContext - Providers schema)

So, users context has 1..1 relationship w/ both Customers and Providers tables.

CustomersContext has several tables related to customers ProvidersContext has several tables related to providers

It is the tables in the Customers + Providers constext's that are getting dumped into the Users (IdentityContext) schema

Upvotes: 0

Views: 1115

Answers (1)

abdusco
abdusco

Reputation: 11091

Whenever EF comes across a property in an entity class A that references B class, it adds a new relation that connects both.

class Student {
    public School School { get; set; } // <-- many-to-one relation
    public List<Course> Courses { get; set; } // <-- many-to-many relation
    // ...
}

This happens by convention and requires no involvement from the developer.

By default, a relationship will be created when there is a navigation property discovered on a type. A property is considered a navigation property if the type it points to can not be mapped as a scalar type by the current database provider.

If you want to prevent EF Core from interpreting these properties as relations, you need to tell it explicitly to ignore those properties and EF won't try to map them to DB.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<AppUser>()
        .Ignore(e => e.CustomerProfile);
    // ...
}

Upvotes: 1

Related Questions