@@ -20,7 +20,7 @@ https://www.cnblogs.com/cqpanda/p/16815263.html
|
||||
5、撤销某次更新到数据库的迁移(自动执行down 方法)
|
||||
dotnet ef database update 某次迁移的前一次迁移名称 -p IRaCIS.Core.Test -c IRCContext
|
||||
|
||||
dotnet ef migrations add RemoveForeignKey -p IRaCIS.Core.Test -c IRCContext -o CodeFirstTest/MSSQL/Migrations
|
||||
dotnet ef migrations add RemoveForeignKey -p IRaCIS.Core.Test -c IRCContext -o CodeFirstTest/MSSQL/Migrations
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,12 @@ namespace IRaCIS.Core.Test.CodeFirstTest.PGSQL.Migrations
|
||||
/// </summary>
|
||||
public class MaxStringLengthConvention : IModelFinalizingConvention
|
||||
{
|
||||
private readonly int _defaultLenth;
|
||||
|
||||
public MaxStringLengthConvention(int defaultLenth)
|
||||
{
|
||||
_defaultLenth = defaultLenth;
|
||||
}
|
||||
public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
|
||||
{
|
||||
foreach (var property in modelBuilder.Metadata.GetEntityTypes()
|
||||
@@ -31,17 +37,17 @@ namespace IRaCIS.Core.Test.CodeFirstTest.PGSQL.Migrations
|
||||
// 输出调试信息,看看是哪种特性生效
|
||||
if (stringLengthAttribute != null)
|
||||
{
|
||||
Console.WriteLine($"{property.Name}: StringLength({stringLengthAttribute.MaximumLength})");
|
||||
//Console.WriteLine($"{property.Name}: StringLength({stringLengthAttribute.MaximumLength})");
|
||||
property.Builder.HasMaxLength(stringLengthAttribute.MaximumLength);
|
||||
}
|
||||
else if (maxLengthAttribute != null)
|
||||
{
|
||||
Console.WriteLine($"{property.Name}: MaxLength (no specific length, allowing max)");
|
||||
//Console.WriteLine($"{property.Name}: MaxLength (no specific length, allowing max)");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"{property.Name}: Default length 200");
|
||||
property.Builder.HasMaxLength(200);
|
||||
//Console.WriteLine($"{property.Name}: Default length {_defaultLenth}");
|
||||
property.Builder.HasMaxLength(_defaultLenth);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public partial class PGContext : DbContext
|
||||
|
||||
//configurationBuilder.Conventions.Add(_ => new NoForeignKeyConvention());
|
||||
//针对字符串使用默认的长度配置
|
||||
configurationBuilder.Conventions.Add(_ => new MaxStringLengthConvention());
|
||||
configurationBuilder.Conventions.Add(_ => new MaxStringLengthConvention(200));
|
||||
}
|
||||
|
||||
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
||||
|
||||
Reference in New Issue
Block a user