/// <summary> /// Adds TenantId to the key and adds a TenantId shadow property on any dependent types' foreign keys. /// </summary> /// <param name="key">The key to adjust for TenantId.</param> /// <param name="modelBuilder">The modelBuilder for the DbContext.</param> /// <returns>The MultiTenantEntityTypeBuilder<T> instance.</returns> public MultiTenantEntityTypeBuilder AdjustKey(IMutableKey key, ModelBuilder modelBuilder) { var propertyNames = key.Properties.Select(p => p.Name).Append("TenantId").ToArray(); var fks = key.GetReferencingForeignKeys().ToList(); if (key.IsPrimaryKey()) { Builder.HasKey(propertyNames); } else { Builder.HasAlternateKey(propertyNames); } foreach (var fk in fks) { var fkEntityBuilder = modelBuilder.Entity(fk.DeclaringEntityType.ClrType); fkEntityBuilder.Property <string>("TenantId"); var props = fk.Properties.Select(p => p.Name).Append("TenantId").ToArray(); fkEntityBuilder.HasOne(fk.PrincipalEntityType.ClrType, fk.DependentToPrincipal.Name) .WithMany(fk.PrincipalToDependent.Name) .HasForeignKey(props) .HasPrincipalKey(propertyNames); } Builder.Metadata.RemoveKey(key.Properties); return(this); }
/// <summary> /// Adds TenantId to the key and adds a TenantId shadow property on any dependent types' foreign keys. /// </summary> /// <param name="key">The key to adjust for TenantId.</param> /// <param name="modelBuilder">The modelBuilder for the DbContext.</param> /// <returns>The MultiTenantEntityTypeBuilder<T> instance.</returns> public MultiTenantEntityTypeBuilder AdjustKey(IMutableKey key, ModelBuilder modelBuilder) { var propertyNames = key.Properties.Select(p => p.Name).Append("TenantId").ToArray(); var fks = key.GetReferencingForeignKeys().ToList(); if (key.IsPrimaryKey()) { // 3.1/5.0 - key and fks replaced on entity // 6.0 - key replaced on entity, fks changed in-place Builder.HasKey(propertyNames); } else { Builder.HasAlternateKey(propertyNames); } foreach (var fk in fks) { var fkEntityBuilder = modelBuilder.Entity(fk.DeclaringEntityType.ClrType); // Note 3.1/5.0 will not have TenantId in Properties, so we add it and set the foreign key. // Note 6.0+ will generate a shadow property with the wrong name in the Properties, we will replace. var props = fk.Properties.Where(p => !p.Name.EndsWith("TenantId")).Select(p => p.Name).Append("TenantId").ToArray(); fkEntityBuilder.Property <string>("TenantId"); fkEntityBuilder.HasOne(fk.PrincipalEntityType.ClrType, fk.DependentToPrincipal?.Name) .WithMany(fk.PrincipalToDependent?.Name) .HasForeignKey(props) .HasPrincipalKey(propertyNames); } Builder.Metadata.RemoveKey(key.Properties); return(this); }