Skip to content

Commit

Permalink
Merge pull request #4 from cioxideru/master
Browse files Browse the repository at this point in the history
ability to skip some enums which should not be in database
  • Loading branch information
pergerch authored Nov 27, 2018
2 parents 7a45d34 + 9441d89 commit 188ae2c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// <copyright file="EnumLockupAttribute.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions
{
using System;

public class EnumLookupAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
continue;
}

if (enumOptions.UseEnumsWithAttributesOnly && !propertyType.GetCustomAttributes(typeof(EnumLookupAttribute), true).Any())
{
continue;
}

IMutableEntityType entityType = property.DeclaringEntityType;

Type concreteType = enumOptions.UseNumberLookup
Expand All @@ -37,8 +42,7 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ public static EnumLookupOptions Default
EnumLookupOptions enumOptions = new EnumLookupOptions();
enumOptions.SetNamingScheme(NamingScheme.SnakeCase);
enumOptions.UseNumberLookup = true;
enumOptions.UseEnumsWithAttributesOnly = false;

return enumOptions;
}
}

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

internal bool UseEnumsWithAttributesOnly { get; private set; }

internal bool UseNumberLookup { get; private set; }

public EnumLookupOptions Pluralize()
Expand All @@ -51,6 +54,13 @@ public EnumLookupOptions Singularize()
return this;
}

public EnumLookupOptions UseEnumsWithAttributeOnly()
{
UseEnumsWithAttributesOnly = true;

return this;
}

public EnumLookupOptions UseNumberAsIdentifier()
{
UseNumberLookup = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<PackageId>SpatialFocus.EntityFrameworkCore.Extensions</PackageId>
<Title>Spatial Focus EntityFrameworkCore Extensions</Title>
<Description>A set of useful extensions for EntityFrameworkCore (Enum Lookup Tables, Naming of tables / properties / keys, Pluralize).</Description>
Expand Down

0 comments on commit 188ae2c

Please sign in to comment.