@@ -0,0 +1,50 @@
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
|
||||
namespace IRaCIS.Core.Test.CodeFirstTest.PGSQL.Migrations
|
||||
{
|
||||
/// <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))))
|
||||
{
|
||||
|
||||
// 获取 MaxLength 特性
|
||||
var maxLengthAttribute = property.PropertyInfo?.GetCustomAttributes(typeof(MaxLengthAttribute), false)
|
||||
.FirstOrDefault() as MaxLengthAttribute;
|
||||
|
||||
// 获取 StringLength 特性
|
||||
var stringLengthAttribute = property.PropertyInfo?.GetCustomAttributes(typeof(StringLengthAttribute), false)
|
||||
.FirstOrDefault() as StringLengthAttribute;
|
||||
|
||||
// 输出调试信息,看看是哪种特性生效
|
||||
if (stringLengthAttribute != null)
|
||||
{
|
||||
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)");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"{property.Name}: Default length 200");
|
||||
property.Builder.HasMaxLength(200);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
|
||||
namespace IRaCIS.Core.Test.CodeFirstTest.PGSQL.Migrations
|
||||
{
|
||||
public class NoForeignKeyMigrationsSqlGenerator : MigrationsSqlGenerator
|
||||
{
|
||||
public NoForeignKeyMigrationsSqlGenerator(
|
||||
MigrationsSqlGeneratorDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void Generate(Microsoft.EntityFrameworkCore.Migrations.Operations.CreateTableOperation operation, IModel? model, MigrationCommandListBuilder builder, bool terminate = true)
|
||||
{
|
||||
operation.ForeignKeys.Clear();
|
||||
base.Generate(operation, model, builder, terminate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IRaCIS.Core.Test.PGModelFolder;
|
||||
namespace IRaCIS.Core.Test.CodeFirstTest.PGSQL.Migrations;
|
||||
|
||||
public abstract class BaseFullAuditEntity
|
||||
{
|
||||
[Key]
|
||||
public Guid Id { get; set; }
|
||||
public Guid CreateUserId { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
public Guid UpdateUserId { get; set; }
|
||||
public DateTime UpdateTime { get; set; }
|
||||
}
|
||||
public class TestStr : BaseFullAuditEntity
|
||||
{
|
||||
public string DefualtLength { get; set; }
|
||||
|
||||
[MaxLength]
|
||||
public string MaxLength { get; set; }
|
||||
|
||||
[StringLength(400)]
|
||||
public string DefineLength { get; set; }
|
||||
}
|
||||
|
||||
public partial class PGContext : DbContext
|
||||
{
|
||||
@@ -15,7 +36,7 @@ public partial class PGContext : DbContext
|
||||
{
|
||||
}
|
||||
|
||||
public virtual DbSet<Subject> Subjects { get; set; }
|
||||
public virtual DbSet<TestStr> TestStr { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder.UseNpgsql("Host=106.14.89.110;Port=5432;Username=sa;Password=pgsql_pwd;Database=Test6_PG");
|
||||
@@ -44,6 +65,13 @@ public partial class PGContext : DbContext
|
||||
|
||||
OnModelCreatingPartial(modelBuilder);
|
||||
}
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
|
||||
//configurationBuilder.Conventions.Add(_ => new NoForeignKeyConvention());
|
||||
//针对字符串使用默认的长度配置
|
||||
configurationBuilder.Conventions.Add(_ => new MaxStringLengthConvention());
|
||||
}
|
||||
|
||||
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace IRaCIS.Core.Test.PGModelFolder;
|
||||
|
||||
public partial class Subject
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[MaxLength(200)]
|
||||
public string LastName { get; set; }
|
||||
|
||||
public int StudyCount { get; set; }
|
||||
|
||||
public DateTime? DeletedTime { get; set; }
|
||||
|
||||
public DateTime? BirthDate { get; set; }
|
||||
|
||||
public DateTime? FirstGiveMedicineTime { get; set; }
|
||||
|
||||
public bool IsMissingImages { get; set; }
|
||||
|
||||
public string MedicalNo { get; set; }
|
||||
[MaxLength(200)]
|
||||
public string ShortName { get; set; }
|
||||
|
||||
public string Reason { get; set; }
|
||||
|
||||
public Guid? FinalSubjectVisitId { get; set; }
|
||||
|
||||
public string Height { get; set; }
|
||||
|
||||
[Comment("subject 编号")]
|
||||
[MaxLength(100)]
|
||||
public string Code { get; set; }
|
||||
|
||||
public int? Age { get; set; }
|
||||
|
||||
public string Modalities { get; set; }
|
||||
|
||||
public DateTime? SignDate { get; set; }
|
||||
|
||||
public DateTime UpdateTime { get; set; }
|
||||
|
||||
public Guid CreateUserId { get; set; }
|
||||
|
||||
public string Sex { get; set; }
|
||||
|
||||
public Guid? LatestSubjectVisitId { get; set; }
|
||||
|
||||
public bool IsEnrollmentConfirm { get; set; }
|
||||
|
||||
public Guid? DeleteUserId { get; set; }
|
||||
|
||||
public string Weight { get; set; }
|
||||
|
||||
public DateTime? OutEnrollmentTime { get; set; }
|
||||
|
||||
public DateTime CreateTime { get; set; }
|
||||
|
||||
public string FirstName { get; set; }
|
||||
|
||||
public bool IsUrgent { get; set; }
|
||||
|
||||
public long Status { get; set; }
|
||||
|
||||
public DateTime? VisitOverTime { get; set; }
|
||||
|
||||
public Guid UpdateUserId { get; set; }
|
||||
|
||||
public bool IsDeleted { get; set; }
|
||||
|
||||
public Guid TrialId { get; set; }
|
||||
|
||||
public bool IsEnrollment { get; set; }
|
||||
|
||||
public bool IsAssignDoctorUser { get; set; }
|
||||
|
||||
public bool IsReReadingOrBackInfluenceAnalysis { get; set; }
|
||||
|
||||
public Guid TrialSiteId { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user