@@ -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);
|
||||
|
||||
@@ -52,16 +52,11 @@
|
||||
<PackageReference Include="Fluid.Core" Version="2.11.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.8" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
-c 指定数据库上下文名字
|
||||
-d 使用数据注解 不指定,默认是fluentAPI
|
||||
-t 指定要生成的表名
|
||||
-p 指定项目名字
|
||||
-p 指定项目名字 (包含上下文,并且产生迁移文件的项目)
|
||||
|
||||
备注: 因为是从数据库反向生成实体,所以会默认生成dbcontext ,每次生成想要的实体后,删除指定的名称context即可
|
||||
针对字符串类型,我们避免string字段 数据库存储null string? 数据库才存储null(除非有合理的理由),这样可以避免代码里面用string 变量,总是要判断是否为null
|
||||
|
||||
Reference in New Issue
Block a user