Skip to content

Commit 188ae2c

Browse files
authored
Merge pull request #4 from cioxideru/master
ability to skip some enums which should not be in database
2 parents 7a45d34 + 9441d89 commit 188ae2c

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// <copyright file="EnumLockupAttribute.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions
7+
{
8+
using System;
9+
10+
public class EnumLookupAttribute : Attribute
11+
{
12+
}
13+
}

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumLookupExtension.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
2626
continue;
2727
}
2828

29+
if (enumOptions.UseEnumsWithAttributesOnly && !propertyType.GetCustomAttributes(typeof(EnumLookupAttribute), true).Any())
30+
{
31+
continue;
32+
}
33+
2934
IMutableEntityType entityType = property.DeclaringEntityType;
3035

3136
Type concreteType = enumOptions.UseNumberLookup
@@ -37,8 +42,7 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
3742
string tableName = enumOptions.NamingFunction(typeName);
3843
enumLookupBuilder.ToTable(tableName);
3944

40-
string keyName = enumOptions.UseNumberLookup
41-
? nameof(EnumWithNumberLookup<Enum>.Id)
45+
string keyName = enumOptions.UseNumberLookup ? nameof(EnumWithNumberLookup<Enum>.Id)
4246
: nameof(EnumWithStringLookup<Enum>.Id);
4347

4448
modelBuilder.Entity(entityType.Name).HasOne(concreteType).WithMany().HasPrincipalKey(keyName).HasForeignKey(property.Name);

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumLookupOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ public static EnumLookupOptions Default
2121
EnumLookupOptions enumOptions = new EnumLookupOptions();
2222
enumOptions.SetNamingScheme(NamingScheme.SnakeCase);
2323
enumOptions.UseNumberLookup = true;
24+
enumOptions.UseEnumsWithAttributesOnly = false;
2425

2526
return enumOptions;
2627
}
2728
}
2829

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

32+
internal bool UseEnumsWithAttributesOnly { get; private set; }
33+
3134
internal bool UseNumberLookup { get; private set; }
3235

3336
public EnumLookupOptions Pluralize()
@@ -51,6 +54,13 @@ public EnumLookupOptions Singularize()
5154
return this;
5255
}
5356

57+
public EnumLookupOptions UseEnumsWithAttributeOnly()
58+
{
59+
UseEnumsWithAttributesOnly = true;
60+
61+
return this;
62+
}
63+
5464
public EnumLookupOptions UseNumberAsIdentifier()
5565
{
5666
UseNumberLookup = true;

src/SpatialFocus.EntityFrameworkCore.Extensions/SpatialFocus.EntityFrameworkCore.Extensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Version>1.0.0</Version>
4+
<Version>1.1.0</Version>
55
<PackageId>SpatialFocus.EntityFrameworkCore.Extensions</PackageId>
66
<Title>Spatial Focus EntityFrameworkCore Extensions</Title>
77
<Description>A set of useful extensions for EntityFrameworkCore (Enum Lookup Tables, Naming of tables / properties / keys, Pluralize).</Description>

0 commit comments

Comments
 (0)