Skip to content

Commit

Permalink
Support for NotMappedAttribute on properties
Browse files Browse the repository at this point in the history
  • Loading branch information
cmenzi committed Dec 19, 2023
1 parent 429cde1 commit 8a683f6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SmartEnum.EFCore
Expand All @@ -23,6 +25,7 @@ public static void ConfigureSmartEnum(this ModelConfigurationBuilder configurati
var propertyTypes = modelBuilder.Model.GetEntityTypes()
.SelectMany(e => e.ClrType.GetProperties())
.Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>)))
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() == null)
.Select(p => p.PropertyType)
.Distinct();

Expand Down Expand Up @@ -52,7 +55,8 @@ public static void ConfigureSmartEnum(this ModelBuilder modelBuilder)
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var properties = entityType.ClrType.GetProperties()
.Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>)));
.Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>)))
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() == null);

foreach (var property in properties)
{
Expand Down
4 changes: 4 additions & 0 deletions test/SmartEnum.EFCore.IntegrationTests/Entities/SomeEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace SmartEnum.EFCore.IntegrationTests.Entities
{
Expand All @@ -24,6 +25,9 @@ public class SomeEntity

public List<SomeOwnedEntity> OwnedEntities { get; set; }

[NotMapped]
public TestEnum NotMappedTest { get; set; }

public class Configuration : IEntityTypeConfiguration<SomeEntity>
{
public void Configure(EntityTypeBuilder<SomeEntity> builder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;

using System.ComponentModel.DataAnnotations.Schema;

namespace SmartEnum.EFCore.IntegrationTests.Entities
{
[Owned]
Expand All @@ -18,5 +20,8 @@ public class SomeOuterOwnedEntity
public TestBaseEnumWithDerivedValues Test4 { get; set; }

public SomeOwnedEntity InnerOwnedEntity { get; set; }

[NotMapped]
public TestEnum NotMappedTest { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;

using System.ComponentModel.DataAnnotations.Schema;

namespace SmartEnum.EFCore.IntegrationTests.Entities
{
[Owned]
Expand All @@ -16,5 +18,8 @@ public class SomeOwnedEntity
public TestStringEnum Test3 { get; set; }

public TestBaseEnumWithDerivedValues Test4 { get; set; }

[NotMapped]
public TestEnum NotMappedTest { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private static SomeEntity CreateEntity()
Test2 = TestDerivedEnum.One,
Test3 = TestStringEnum.One,
Test4 = DerivedTestEnumWithValues1.A,
NotMappedTest = TestEnum.One,
OwnedEntity = new SomeOwnedEntity
{
Value = 2,
Expand Down Expand Up @@ -143,27 +144,31 @@ private static void VerifyEntity(SomeEntity entity)
entity.Test2.Should().Be(TestDerivedEnum.One);
entity.Test3.Should().Be(TestStringEnum.One);
entity.Test4.Should().Be(DerivedTestEnumWithValues1.A);
entity.NotMappedTest.Should().BeNull();

entity.OwnedEntity.Value.Should().Be(2);
entity.OwnedEntity.Weekday.Should().Be(Weekday.Friday);
entity.OwnedEntity.Test1.Should().Be(TestEnum.Two);
entity.OwnedEntity.Test2.Should().Be(TestDerivedEnum.One);
entity.OwnedEntity.Test3.Should().Be(TestStringEnum.Two);
entity.OwnedEntity.Test4.Should().Be(DerivedTestEnumWithValues1.B);
entity.OwnedEntity.NotMappedTest.Should().BeNull();

entity.OuterOwnedEntity.Value.Should().Be(3);
entity.OuterOwnedEntity.Weekday.Should().Be(Weekday.Friday);
entity.OuterOwnedEntity.Test1.Should().Be(TestEnum.Two);
entity.OuterOwnedEntity.Test2.Should().Be(TestDerivedEnum.One);
entity.OuterOwnedEntity.Test3.Should().Be(TestStringEnum.Two);
entity.OuterOwnedEntity.Test4.Should().Be(DerivedTestEnumWithValues1.B);
entity.OuterOwnedEntity.NotMappedTest.Should().BeNull();

entity.OuterOwnedEntity.InnerOwnedEntity.Value.Should().Be(4);
entity.OuterOwnedEntity.InnerOwnedEntity.Weekday.Should().Be(Weekday.Friday);
entity.OuterOwnedEntity.InnerOwnedEntity.Test1.Should().Be(TestEnum.Two);
entity.OuterOwnedEntity.InnerOwnedEntity.Test2.Should().Be(TestDerivedEnum.One);
entity.OuterOwnedEntity.InnerOwnedEntity.Test3.Should().Be(TestStringEnum.Two);
entity.OuterOwnedEntity.InnerOwnedEntity.Test4.Should().Be(DerivedTestEnumWithValues1.B);
entity.OuterOwnedEntity.InnerOwnedEntity.NotMappedTest.Should().BeNull();

entity.OwnedEntities.Should().SatisfyRespectively(
o =>
Expand All @@ -174,6 +179,7 @@ private static void VerifyEntity(SomeEntity entity)
o.Test2.Should().Be(TestDerivedEnum.One);
o.Test3.Should().Be(TestStringEnum.Three);
o.Test4.Should().Be(DerivedTestEnumWithValues1.A);
o.NotMappedTest.Should().BeNull();
},
o =>
{
Expand All @@ -183,6 +189,7 @@ private static void VerifyEntity(SomeEntity entity)
o.Test2.Should().Be(TestDerivedEnum.One);
o.Test3.Should().Be(TestStringEnum.One);
o.Test4.Should().Be(DerivedTestEnumWithValues1.B);
o.NotMappedTest.Should().BeNull();
});
}
}
Expand Down

0 comments on commit 8a683f6

Please sign in to comment.