diff --git a/IRaCIS.Core.Domain/Trial/Trial.cs b/IRaCIS.Core.Domain/Trial/Trial.cs index f80e1657b..184c754a0 100644 --- a/IRaCIS.Core.Domain/Trial/Trial.cs +++ b/IRaCIS.Core.Domain/Trial/Trial.cs @@ -13,13 +13,15 @@ namespace IRaCIS.Core.Domain.Models { #region 导航属性 [JsonIgnore] + [ForeignKey("SponsorId")] + public Sponsor Sponsor { get; set; } + [JsonIgnore] + [ForeignKey("CROId")] + public CRO CRO { get; set; } + [JsonIgnore] public List TrialBodyPartList { get; set; } - - [JsonIgnore] public List TaskMedicalReviewList { get; set; } - - [JsonIgnore] public List TaskConsistentRuleList { get; set; } [JsonIgnore] @@ -56,21 +58,12 @@ namespace IRaCIS.Core.Domain.Models public List ReadModuleList { get; set; } = new List(); [JsonIgnore] public List UserFeedBackList { get; set; } = new List(); - [JsonIgnore] [ForeignKey("IndicationTypeId")] public Dictionary IndicationType { get; set; } - [JsonIgnore] [ForeignKey("PhaseId")] public Dictionary Phase { get; set; } - - [JsonIgnore] - [ForeignKey("SponsorId")] - public Sponsor Sponsor { get; set; } - [JsonIgnore] - [ForeignKey("CROId")] - public CRO CRO { get; set; } [JsonIgnore] [ForeignKey("ReviewModeId")] public Dictionary ReviewMode { get; set; } diff --git a/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 b/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 index f2828578e..0693d6509 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 +++ b/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 @@ -21,3 +21,12 @@ https://www.cnblogs.com/cqpanda/p/16815263.html dotnet ef database update ijǨƵǰһǨ -p IRaCIS.Core.Test -c IRCContext dotnet ef migrations add RemoveForeignKey -p IRaCIS.Core.Test -c IRCContext -o CodeFirstTest/MSSQL/Migrations + + + + + + PGSQL ָ + dotnet ef migrations add testSTR -p IRaCIS.Core.Test -c PGContext -o CodeFirstTest/PGSQL/Migrations + + dotnet ef migrations remove -p IRaCIS.Core.Test -c PGContext \ No newline at end of file diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/MaxStringLengthConvention .cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/MaxStringLengthConvention .cs index a7ab7624a..f7198ecad 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/MaxStringLengthConvention .cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/MaxStringLengthConvention .cs @@ -1,5 +1,7 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Conventions; +using System; +using System.ComponentModel.DataAnnotations; using System.Linq; namespace IRaCIS.Core.Test @@ -17,7 +19,30 @@ namespace IRaCIS.Core.Test .Where( property => property.ClrType == typeof(string)))) { - property.Builder.HasMaxLength(200); + + // 获取 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); + } } } } diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs index 2792bb81c..d89991305 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs @@ -26,11 +26,14 @@ public partial class IRCContext : DbContext //public virtual DbSet ReadModule { get; set; } //public virtual DbSet TestStringLength { get; set; } + + + public virtual DbSet TestStr { get; set; } public virtual DbSet Project { get; set; } public virtual DbSet ProjectUser { get; set; } public virtual DbSet ProjectUser2 { get; set; } - + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.Designer.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.Designer.cs new file mode 100644 index 000000000..d0f7385ae --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.Designer.cs @@ -0,0 +1,146 @@ +// +using System; +using IRaCIS.Core.Test.CodeFirstTest.MSSQL; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations +{ + [DbContext(typeof(IRCContext))] + [Migration("20240912145208_test")] + partial class test + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Project", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("Project"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Code") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("Name2") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("OtherName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ProjectId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("ProjectId"); + + b.ToTable("ProjectUser"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser2", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("ProjectId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("ProjectId"); + + b.ToTable("ProjectUser2"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Project", "Project") + .WithMany() + .HasForeignKey("ProjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Project"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser2", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Project", "Project") + .WithMany() + .HasForeignKey("ProjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Project"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.cs new file mode 100644 index 000000000..f4e031f05 --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.cs @@ -0,0 +1,40 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations +{ + /// + public partial class test : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "Name", + table: "ProjectUser", + newName: "OtherName"); + + migrationBuilder.AddColumn( + name: "Name2", + table: "ProjectUser", + type: "nvarchar(200)", + maxLength: 200, + nullable: false, + defaultValue: ""); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Name2", + table: "ProjectUser"); + + migrationBuilder.RenameColumn( + name: "OtherName", + table: "ProjectUser", + newName: "Name"); + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.Designer.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.Designer.cs new file mode 100644 index 000000000..9c516cb77 --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.Designer.cs @@ -0,0 +1,183 @@ +// +using System; +using IRaCIS.Core.Test.CodeFirstTest.MSSQL; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations +{ + [DbContext(typeof(IRCContext))] + [Migration("20240912152420_testSTR")] + partial class testSTR + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Project", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("Project"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("Code") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("Name2") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("OtherName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ProjectId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("ProjectId"); + + b.ToTable("ProjectUser"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser2", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("ProjectId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("ProjectId"); + + b.ToTable("ProjectUser2"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestStr", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("DefineLength") + .IsRequired() + .HasMaxLength(400) + .HasColumnType("nvarchar(400)"); + + b.Property("DefualtLength") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("MaxLength") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("TestStr"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Project", "Project") + .WithMany() + .HasForeignKey("ProjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Project"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser2", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Project", "Project") + .WithMany() + .HasForeignKey("ProjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Project"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.cs new file mode 100644 index 000000000..6b04291ac --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.cs @@ -0,0 +1,40 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations +{ + /// + public partial class testSTR : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "TestStr", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + DefualtLength = table.Column(type: "nvarchar(200)", maxLength: 200, nullable: false), + MaxLength = table.Column(type: "nvarchar(max)", nullable: false), + DefineLength = table.Column(type: "nvarchar(400)", maxLength: 400, nullable: false), + CreateUserId = table.Column(type: "uniqueidentifier", nullable: false), + CreateTime = table.Column(type: "datetime2", nullable: false), + UpdateUserId = table.Column(type: "uniqueidentifier", nullable: false), + UpdateTime = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TestStr", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "TestStr"); + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs index 2a1d0e054..58210d549 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs @@ -42,7 +42,7 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations b.HasKey("Id"); - b.ToTable("Project", (string)null); + b.ToTable("Project"); }); modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser", b => @@ -62,7 +62,12 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations b.Property("CreateUserId") .HasColumnType("uniqueidentifier"); - b.Property("Name") + b.Property("Name2") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("OtherName") .IsRequired() .HasMaxLength(200) .HasColumnType("nvarchar(200)"); @@ -80,7 +85,7 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations b.HasIndex("ProjectId"); - b.ToTable("ProjectUser", (string)null); + b.ToTable("ProjectUser"); }); modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser2", b => @@ -108,7 +113,44 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations b.HasIndex("ProjectId"); - b.ToTable("ProjectUser2", (string)null); + b.ToTable("ProjectUser2"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestStr", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("DefineLength") + .IsRequired() + .HasMaxLength(400) + .HasColumnType("nvarchar(400)"); + + b.Property("DefualtLength") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("MaxLength") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("TestStr"); }); modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser", b => diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs index 876fedd1c..1a9805d06 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs @@ -180,5 +180,19 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL } #endregion + #region 测试字符串长度 + + public class TestStr: BaseFullAuditEntity + { + public string DefualtLength { get; set; } + + [MaxLength] + public string MaxLength { get; set; } + + [StringLength(400)] + public string DefineLength { get; set;} + } + #endregion + } diff --git a/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Convention/MaxStringLengthConvention .cs b/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Convention/MaxStringLengthConvention .cs new file mode 100644 index 000000000..e59564802 --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Convention/MaxStringLengthConvention .cs @@ -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 +{ + /// + /// Efcore 最新支持批量配置字符串类型长度作为保底的 官网参考:https://learn.microsoft.com/zh-cn/ef/core/modeling/bulk-configuration#conventions + /// + public class MaxStringLengthConvention : IModelFinalizingConvention + { + public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext 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); + } + } + } + } + +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Convention/NoForeignKeyMigrationsSqlGenerator.cs b/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Convention/NoForeignKeyMigrationsSqlGenerator.cs new file mode 100644 index 000000000..ba493df11 --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Convention/NoForeignKeyMigrationsSqlGenerator.cs @@ -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); + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/PGSQL/PGContext.cs b/IRaCIS.Core.Test/CodeFirstTest/PGSQL/PGContext.cs index 73dffe521..0437fa656 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/PGSQL/PGContext.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/PGSQL/PGContext.cs @@ -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 Subjects { get; set; } + public virtual DbSet 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); } diff --git a/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Subject.cs b/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Subject.cs deleted file mode 100644 index 8ac7a14d8..000000000 --- a/IRaCIS.Core.Test/CodeFirstTest/PGSQL/Subject.cs +++ /dev/null @@ -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; } -} diff --git a/IRaCIS.Core.Test/Program.cs b/IRaCIS.Core.Test/Program.cs index b2b5d4a75..64f171a7c 100644 --- a/IRaCIS.Core.Test/Program.cs +++ b/IRaCIS.Core.Test/Program.cs @@ -58,9 +58,9 @@ partial class Program // 要生成的表名数组 var tableNames = new List(args); - var argsStr = args.ToString()??string.Empty; + var argsStr = string.Join("", args) ??string.Empty; - if (argsStr.Contains(".") || argsStr.Contains("-")|| argsStr.Contains("application")|| argsStr.Contains("IR")) + if ( argsStr.Contains("-")|| argsStr.Contains("application") || argsStr.Contains("Test") || argsStr.Contains(".")) { tableNames = new List(); }