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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
string tableName = enumOptions.NamingFunction(typeName);
enumLookupBuilder.ToTable(tableName);

string keyName = enumOptions.UseNumberLookup ? nameof(EnumWithNumberLookup<Enum>.Id)
string keyName = enumOptions.UseNumberLookup
? nameof(EnumWithNumberLookup<Enum>.Id)
: nameof(EnumWithStringLookup<Enum>.Id);

modelBuilder.Entity(entityType.Name).HasOne(concreteType).WithMany().HasPrincipalKey(keyName).HasForeignKey(property.Name);
modelBuilder.Entity(entityType.Name)
.HasOne(concreteType)
.WithMany()
.HasPrincipalKey(keyName)
.HasForeignKey(property.Name)
.OnDelete(enumOptions.DeleteBehavior);

if (enumOptions.UseNumberLookup)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace SpatialFocus.EntityFrameworkCore.Extensions
{
using System;
using Humanizer;
using Microsoft.EntityFrameworkCore;

public class EnumLookupOptions
{
Expand All @@ -22,13 +23,16 @@ public static EnumLookupOptions Default
enumOptions.SetNamingScheme(NamingScheme.SnakeCase);
enumOptions.UseNumberLookup = true;
enumOptions.UseEnumsWithAttributesOnly = false;
enumOptions.DeleteBehavior = DeleteBehavior.Cascade;

return enumOptions;
}
}

internal Func<string, string> NamingFunction => name => this.postProcessingTableNamingFunction(this.namingFunction(name));

internal DeleteBehavior DeleteBehavior { get; private set; }

internal bool UseEnumsWithAttributesOnly { get; private set; }

internal bool UseNumberLookup { get; private set; }
Expand All @@ -40,6 +44,13 @@ public EnumLookupOptions Pluralize()
return this;
}

public EnumLookupOptions SetDeleteBehavior(DeleteBehavior deleteBehavior)
{
DeleteBehavior = deleteBehavior;

return this;
}

public EnumLookupOptions SetNamingScheme(Func<string, string> namingFunc)
{
this.namingFunction = namingFunc;
Expand Down