irc-netcore-api/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/MaxStringLengthConvention .cs

26 lines
1019 B
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using System.Linq;
namespace IRaCIS.Core.Test
{
/// <summary>
/// Efcore 最新支持批量配置字符串类型长度作为保底的 官网参考https://learn.microsoft.com/zh-cn/ef/core/modeling/bulk-configuration#conventions
/// </summary>
public class MaxStringLengthConvention : IModelFinalizingConvention
{
public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
{
foreach (var property in modelBuilder.Metadata.GetEntityTypes()
.SelectMany(
entityType => entityType.GetDeclaredProperties()
.Where(
property => property.ClrType == typeof(string))))
{
property.Builder.HasMaxLength(200);
}
}
}
}