-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for convention-based max length in EF Core
- Loading branch information
1 parent
dcbd0bd
commit 5110be9
Showing
16 changed files
with
160 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/AD.BaseTypes.EFCore/Conventions/BaseTypeMaxLengthConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using AD.BaseTypes.EFCore.Extensions; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
|
||
namespace AD.BaseTypes.EFCore.Conventions; | ||
|
||
/// <summary> | ||
/// Configures the maximum length of data that can be stored in all <see cref="T:IBaseType<TBaseType,string>"/> | ||
/// properties when a model is being finalized. | ||
/// </summary> | ||
/// <remarks> | ||
/// Supported attributes are: | ||
/// <list type="bullet"> | ||
/// <item><description><see cref="MaxLengthStringAttribute"/></description></item> | ||
/// <item><description><see cref="MinMaxLengthStringAttribute"/></description></item> | ||
/// </list> | ||
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples. | ||
/// </remarks> | ||
public class BaseTypeMaxLengthConvention : IModelFinalizingConvention | ||
{ | ||
/// <summary> | ||
/// Called when a model is being finalized. | ||
/// </summary> | ||
/// <param name="modelBuilder">The builder for the model.</param> | ||
/// <param name="context">Additional information associated with convention execution.</param> | ||
public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, | ||
IConventionContext<IConventionModelBuilder> context) | ||
{ | ||
foreach (var baseTypeProperty in modelBuilder.GetBaseTypeConventionProperties()) | ||
{ | ||
if (Attribute.GetCustomAttribute(baseTypeProperty.ClrType, typeof(MaxLengthStringAttribute)) is | ||
MaxLengthStringAttribute maxLengthStringAttribute) | ||
{ | ||
baseTypeProperty.Builder.HasMaxLength(maxLengthStringAttribute.MaxLength); | ||
continue; | ||
} | ||
|
||
if (Attribute.GetCustomAttribute(baseTypeProperty.ClrType, typeof(MinMaxLengthStringAttribute)) is | ||
MinMaxLengthStringAttribute minMaxLengthStringAttribute) | ||
{ | ||
baseTypeProperty.Builder.HasMaxLength(minMaxLengthStringAttribute.MaxLength); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/AD.BaseTypes.EFCore/Extensions/ConventionModelBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using AD.BaseTypes.Extensions; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace AD.BaseTypes.EFCore.Extensions; | ||
|
||
internal static class ConventionModelBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Gets all non-navigation properties implementing <see cref="IBaseType{TBaseType,TWrapped}"/> declared on the entity type. | ||
/// </summary> | ||
/// <param name="modelBuilder">Convention model builder</param> | ||
/// <returns>Declared non-navigation properties implementing <see cref="IBaseType{TBaseType,TWrapped}"/>.</returns> | ||
public static IEnumerable<IConventionProperty> GetBaseTypeConventionProperties(this IConventionModelBuilder modelBuilder) => | ||
modelBuilder.Metadata | ||
.GetEntityTypes() | ||
.SelectMany(x => x.GetDeclaredProperties()) | ||
.Where(x => x.ClrType.ImplementsIBaseType()); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/AD.BaseTypes.EFCore/Extensions/ConventionSetBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using AD.BaseTypes.EFCore.Conventions; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace AD.BaseTypes.EFCore.Extensions; | ||
|
||
/// <summary> | ||
/// Convention set builder extensions. | ||
/// </summary> | ||
public static class ConventionSetBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Apply base type conventions to all <see cref="IBaseType{TBaseType,TWrapped}"/> properties when a model is being finalized. | ||
/// </summary> | ||
/// <param name="conventionSetBuilder">Builder for configuring conventions.</param> | ||
/// <remarks> | ||
/// Conventions applied are: | ||
/// <list type="bullet"> | ||
/// <item><description><see cref="BaseTypeConversionConvention"/></description></item> | ||
/// <item><description><see cref="BaseTypeMaxLengthConvention"/></description></item> | ||
/// </list> | ||
/// </remarks> | ||
/// <returns>The convention set builder</returns> | ||
public static ConventionSetBuilder AddBaseTypeConventions(this ConventionSetBuilder conventionSetBuilder) => | ||
conventionSetBuilder | ||
.AddBaseTypeConversionConvention() | ||
.AddBaseTypeMaxLengthConvention(); | ||
|
||
/// <summary> | ||
/// Apply the value converter <see cref="BaseTypeValueConverter{TBaseType, TWrapped}"/> as a convention | ||
/// to all <see cref="IBaseType{TBaseType,TWrapped}"/> properties when a model is being finalized. | ||
/// </summary> | ||
/// <param name="conventionSetBuilder">Builder for configuring conventions.</param> | ||
/// <returns>The convention set builder</returns> | ||
public static ConventionSetBuilder AddBaseTypeConversionConvention(this ConventionSetBuilder conventionSetBuilder) | ||
{ | ||
conventionSetBuilder.Add(_ => new BaseTypeConversionConvention()); | ||
return conventionSetBuilder; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the maximum length of data that can be stored in all<see cref="T:IBaseType<TBaseType,string>"/> | ||
/// properties when a model is being finalized. | ||
/// </summary> | ||
/// <param name="conventionSetBuilder">Builder for configuring conventions.</param> | ||
/// <returns>The convention set builder</returns> | ||
public static ConventionSetBuilder AddBaseTypeMaxLengthConvention(this ConventionSetBuilder conventionSetBuilder) | ||
{ | ||
conventionSetBuilder.Add(_ => new BaseTypeMaxLengthConvention()); | ||
return conventionSetBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using TestApp.Data.Infrastructure; | ||
using TestApp.Infrastructure; | ||
|
||
namespace TestApp.Web; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/TestApp/Migrations/20221201084034_InitialCreate.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters