Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetDeleteBehavior option to EnumLookupOptions #9

Merged
merged 2 commits into from
Jan 4, 2019

Conversation

BalintBanyasz
Copy link
Contributor

@BalintBanyasz BalintBanyasz commented Dec 28, 2018

EF Core DeleteBehavior defaults to DeleteBehavior.Cascade and currently there is no way to change this setting globally. The new SetDeleteBehavior option allows users to specify the DeleteBehavior for the generated FKs while keeping Cascade as the default behavior for compatibility.

One potential use case is when introducing the auto-generated FK with the default Cascade behavior would result in an error with SQL Server (Introducing FOREIGN KEY constraint 'fk_two' on table 'table2' may cause cycles or multiple cascade paths...).

@pergerch pergerch merged commit f66b320 into SpatialFocus:master Jan 4, 2019
@pergerch
Copy link
Member

pergerch commented Jan 4, 2019

Thanks for your contribution. This is now included in release 1.3.0

@TAGC
Copy link
Contributor

TAGC commented Jun 4, 2021

Is there a way to override this for a single enum relationship while keeping the default FK delete behaviour for enums as Cascade? E.g.

public class ProfileRecordConfiguration : IEntityTypeConfiguration<ProfileRecord>
{
    public void Configure(EntityTypeBuilder<ProfileRecord> builder)
    {
        builder.ToTable(nameof(ProfileRecord));

        builder.HasKey(nameof(ProductConsultant) + "Id", nameof(ProfileRecord.Period));

        builder.Property(x => x.Period).HasConversion(
            x => (int) x,
            x => new Period(x));

        builder.HasOne<ProductConsultant>().WithMany();
        builder.HasOne(x => x.CommissionPlan).WithMany().OnDelete(DeleteBehavior.Restrict);

        // VehicleSaleType property has enum type PrimaryVehicleSaleType, and should use a different
        // FK deletion behaviour from the default enum deletion behaviour of `Cascade`
        //builder.HasOne(x => x.VehicleSaleType).WithMany().OnDelete(DeleteBehavior.Restrict);
    }
}

@TAGC
Copy link
Contributor

TAGC commented Jun 4, 2021

An ugly workaround that I'm using at present:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var enumLookupOptions = EnumLookupOptions.Default.SetNamingScheme(x => x.Pascalize());

    modelBuilder.ApplyConfigurationsFromAssembly(typeof(CommissionsContext).Assembly);
    // ...
    modelBuilder.ConfigureEnumLookup(enumLookupOptions);
    FixProfileRecordConfiguration(modelBuilder);
    // ...
}

private static void FixProfileRecordConfiguration(ModelBuilder modelBuilder)
{
    var profileRecordBuilder = modelBuilder.Entity<ProfileRecord>();

    var primaryVehicleSaleTypeRelationship = profileRecordBuilder.Metadata.GetForeignKeys()
        .Single(x => x.PrincipalEntityType.ClrType == typeof(EnumWithNumberLookup<PrimaryVehicleSaleType>));

    primaryVehicleSaleTypeRelationship.DeleteBehavior = DeleteBehavior.Restrict;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants