diff --git a/IRaCIS.Core.Domain/Visit/SubjectVisit.cs b/IRaCIS.Core.Domain/Visit/SubjectVisit.cs index 44e477ee1..2e7a566fa 100644 --- a/IRaCIS.Core.Domain/Visit/SubjectVisit.cs +++ b/IRaCIS.Core.Domain/Visit/SubjectVisit.cs @@ -16,8 +16,6 @@ namespace IRaCIS.Core.Domain.Models [JsonIgnore] public TrialSite TrialSite { get; set; } - [JsonIgnore] - public VisitStage VisitStage { get; set; } [JsonIgnore] [ForeignKey("OutPlanPreviousVisitId")] public SubjectVisit OutPlanPreviousVisit { get; set; } diff --git a/IRaCIS.Core.Infra.EFCore/EntityConfigration/DictionaryConfigration.cs b/IRaCIS.Core.Infra.EFCore/EntityConfigration/DictionaryConfigration.cs index 1326060fc..9d77cb761 100644 --- a/IRaCIS.Core.Infra.EFCore/EntityConfigration/DictionaryConfigration.cs +++ b/IRaCIS.Core.Infra.EFCore/EntityConfigration/DictionaryConfigration.cs @@ -16,8 +16,8 @@ namespace IRaCIS.Core.Infra.EFCore.EntityConfigration public void Configure(EntityTypeBuilder builder) { - - builder.HasMany(t => t.ChildList).WithOne(t => t.Parent); + //自身同时存在一对多 和一对一的关系,配置一对多的就可以,一对一 不用配置,有点奇怪 + builder.HasMany(t => t.ChildList).WithOne(t => t.Parent).HasForeignKey(d => d.ParentId); } } diff --git a/IRaCIS.Core.Infra.EFCore/EntityConfigration/SubjectConfigration.cs b/IRaCIS.Core.Infra.EFCore/EntityConfigration/SubjectConfigration.cs index 239c349da..87893ad17 100644 --- a/IRaCIS.Core.Infra.EFCore/EntityConfigration/SubjectConfigration.cs +++ b/IRaCIS.Core.Infra.EFCore/EntityConfigration/SubjectConfigration.cs @@ -13,9 +13,9 @@ namespace IRaCIS.Core.Infra.EFCore.EntityConfigration { //不能同时配置一对多 和一对一 但是有时表要存储多的最新的 比如受试者 最新的访视 在这里要显示配置 - builder.HasOne(s => s.LatestSubjectVisit); - builder.HasOne(s => s.FinalSubjectVisit); - builder.HasMany(s => s.SubjectVisitList).WithOne(sv => sv.Subject); + builder.HasOne(s => s.LatestSubjectVisit).WithMany().HasForeignKey(t => t.LatestSubjectVisitId); + builder.HasOne(s => s.FinalSubjectVisit).WithMany().HasForeignKey(t => t.FinalSubjectVisitId); + builder.HasMany(s => s.SubjectVisitList).WithOne(sv => sv.Subject).HasForeignKey(t => t.SubjectId); } } } diff --git a/IRaCIS.Core.Infra.EFCore/EntityConfigration/SubjectVisitConfigration.cs b/IRaCIS.Core.Infra.EFCore/EntityConfigration/SubjectVisitConfigration.cs deleted file mode 100644 index d916b4ee1..000000000 --- a/IRaCIS.Core.Infra.EFCore/EntityConfigration/SubjectVisitConfigration.cs +++ /dev/null @@ -1,16 +0,0 @@ -using IRaCIS.Core.Domain.Models; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - - -namespace IRaCIS.Core.Infra.EFCore.EntityConfigration -{ - public class SubjectVisitConfigration : IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) - { - - builder.HasOne(s => s.Subject).WithMany(sv => sv.SubjectVisitList); - } - } -} diff --git a/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 b/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 index 33b575bf1..5a329af9d 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 +++ b/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 @@ -2,13 +2,14 @@ #οѧϰĵ https://www.cnblogs.com/cqpanda/p/16815263.html -# ָĿƣҪĿĿ¼ һpwershellĿĿ¼ҪָĿ - cd .\IRaCIS.Core.Test - - # Ŀ¼жģҪֶָ # dotnet ef migrations add ǩ -p Ŀ -c -o ǨļĿ¼ + 1Ǩļ dotnet ef migrations add Initial -p IRaCIS.Core.Test -c IRCContext -o CodeFirstTest/MSSQL/Migrations + 2ղɵǨļ + dotnet ef migrations remove -p IRaCIS.Core.Test -c IRCContext + + 3Ǩļµݿ dotnet ef database update -p IRaCIS.Core.Test -c IRCContext \ No newline at end of file diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/EntityConfigration/DictionaryConfigration.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/EntityConfigration/DictionaryConfigration.cs new file mode 100644 index 000000000..07ea337df --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/EntityConfigration/DictionaryConfigration.cs @@ -0,0 +1,24 @@ +using IRaCIS.Core.Domain.Models; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL +{ + public class DictionaryConfigration : IEntityTypeConfiguration + { + + + public void Configure(EntityTypeBuilder builder) + { + + + builder.HasMany(t => t.ChildList).WithOne(t => t.Parent).HasForeignKey(d => d.ParentId); + + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/EntityConfigration/SubjectConfigration.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/EntityConfigration/SubjectConfigration.cs new file mode 100644 index 000000000..888935f60 --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/EntityConfigration/SubjectConfigration.cs @@ -0,0 +1,33 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + + +namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL +{ + public class SubjectConfigration : IEntityTypeConfiguration + { + + + public void Configure(EntityTypeBuilder builder) + { + + //不能同时配置一对多 和一对一 但是有时表要存储多的最新的 比如受试者 最新的访视 在这里要显示配置 + builder.HasOne(s => s.LatestSubjectVisit); + builder.HasOne(s => s.FinalSubjectVisit); + builder.HasMany(s => s.SubejectVisitList).WithOne(sv => sv.Subject); + } + } + + public class TestOneConfigration : IEntityTypeConfiguration + { + + + public void Configure(EntityTypeBuilder builder) + { + + builder.HasOne(s => s.LatestTestMany).WithMany().HasForeignKey(t=>t.LatestTestManyId); + builder.HasOne(s => s.FinalTestMany).WithMany().HasForeignKey(t => t.FinalTestManyId); + builder.HasMany(s => s.TestManyList).WithOne(sv => sv.TestOne).HasForeignKey(t => t.TestOneId); + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs index 4f6220861..01eb69f95 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection; using IRaCIS.Core.Test.CodeFirstTest.MSSQL; using Microsoft.EntityFrameworkCore; @@ -24,13 +26,23 @@ public partial class IRCContext : DbContext public virtual DbSet SubejectVisit { get; set; } + public virtual DbSet Dictionary { get; set; } + public virtual DbSet TestOne { get; set; } + public virtual DbSet TestMany { get; set; } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer("Server=106.14.89.110,1433;Database=IRC_Code;User ID=sa;Password=mssql_KnTs2a;TrustServerCertificate=true"); protected override void OnModelCreating(ModelBuilder modelBuilder) { + //遍历实体模型手动配置 + var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null); + foreach (var type in typesToRegister) + { + dynamic configurationInstance = Activator.CreateInstance(type); + modelBuilder.ApplyConfiguration(configurationInstance); + } - OnModelCreatingPartial(modelBuilder); } diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910073024_oneToManyAndOneToOne.Designer.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910073024_oneToManyAndOneToOne.Designer.cs new file mode 100644 index 000000000..20cdc133f --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910073024_oneToManyAndOneToOne.Designer.cs @@ -0,0 +1,213 @@ +// +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("20240910073024_oneToManyAndOneToOne")] + partial class oneToManyAndOneToOne + { + /// + 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.SubejectVisit", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("SubjectId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("SubjectId"); + + b.ToTable("SubejectVisit"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("FinalSubjectVisitId") + .HasColumnType("uniqueidentifier"); + + b.Property("LatestSubjectVisitId") + .HasColumnType("uniqueidentifier"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("FinalSubjectVisitId"); + + b.HasIndex("LatestSubjectVisitId"); + + b.ToTable("Subject"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestNew", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("TestName") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("TrialImageDownloadId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("TrialImageDownloadId"); + + b.ToTable("TestNew"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TrialImageDownload", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("DownloadEndTime") + .HasColumnType("datetime2"); + + b.Property("DownloadStartTime") + .HasColumnType("datetime2"); + + b.Property("ImageCount") + .HasColumnType("int"); + + b.Property("ImageSize") + .HasColumnType("bigint"); + + b.Property("ImageType") + .HasColumnType("int"); + + b.Property("IsSuccess") + .HasColumnType("bit"); + + b.Property("SubjectVisitId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("TrialImageDownload"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", "Subject") + .WithMany("SubejectVisitList") + .HasForeignKey("SubjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Subject"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", "FinalSubjectVisit") + .WithMany() + .HasForeignKey("FinalSubjectVisitId"); + + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", "LatestSubjectVisit") + .WithMany() + .HasForeignKey("LatestSubjectVisitId"); + + b.Navigation("FinalSubjectVisit"); + + b.Navigation("LatestSubjectVisit"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestNew", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TrialImageDownload", null) + .WithMany("TestNewList") + .HasForeignKey("TrialImageDownloadId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.Navigation("SubejectVisitList"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TrialImageDownload", b => + { + b.Navigation("TestNewList"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910073024_oneToManyAndOneToOne.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910073024_oneToManyAndOneToOne.cs new file mode 100644 index 000000000..3627f081b --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910073024_oneToManyAndOneToOne.cs @@ -0,0 +1,96 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations +{ + /// + public partial class oneToManyAndOneToOne : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "SubejectVisit", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + SubjectId = table.Column(type: "uniqueidentifier", 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_SubejectVisit", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Subject", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + Name = table.Column(type: "nvarchar(max)", nullable: false), + LatestSubjectVisitId = table.Column(type: "uniqueidentifier", nullable: true), + FinalSubjectVisitId = table.Column(type: "uniqueidentifier", nullable: true), + 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_Subject", x => x.Id); + table.ForeignKey( + name: "FK_Subject_SubejectVisit_FinalSubjectVisitId", + column: x => x.FinalSubjectVisitId, + principalTable: "SubejectVisit", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_Subject_SubejectVisit_LatestSubjectVisitId", + column: x => x.LatestSubjectVisitId, + principalTable: "SubejectVisit", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_SubejectVisit_SubjectId", + table: "SubejectVisit", + column: "SubjectId"); + + migrationBuilder.CreateIndex( + name: "IX_Subject_FinalSubjectVisitId", + table: "Subject", + column: "FinalSubjectVisitId"); + + migrationBuilder.CreateIndex( + name: "IX_Subject_LatestSubjectVisitId", + table: "Subject", + column: "LatestSubjectVisitId"); + + migrationBuilder.AddForeignKey( + name: "FK_SubejectVisit_Subject_SubjectId", + table: "SubejectVisit", + column: "SubjectId", + principalTable: "Subject", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_SubejectVisit_Subject_SubjectId", + table: "SubejectVisit"); + + migrationBuilder.DropTable( + name: "Subject"); + + migrationBuilder.DropTable( + name: "SubejectVisit"); + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910075136_seftOneToManyAndOneToOne.Designer.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910075136_seftOneToManyAndOneToOne.Designer.cs new file mode 100644 index 000000000..69d7b8786 --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910075136_seftOneToManyAndOneToOne.Designer.cs @@ -0,0 +1,269 @@ +// +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("20240910075136_seftOneToManyAndOneToOne")] + partial class seftOneToManyAndOneToOne + { + /// + 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.Dictionary", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("Code") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ConfigTypeId") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("ParentId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("ConfigTypeId"); + + b.HasIndex("ParentId"); + + b.ToTable("Dictionary"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("SubjectId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("SubjectId"); + + b.ToTable("SubejectVisit"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("FinalSubjectVisitId") + .HasColumnType("uniqueidentifier"); + + b.Property("LatestSubjectVisitId") + .HasColumnType("uniqueidentifier"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("FinalSubjectVisitId"); + + b.HasIndex("LatestSubjectVisitId"); + + b.ToTable("Subject"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestNew", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("TestName") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("TrialImageDownloadId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("TrialImageDownloadId"); + + b.ToTable("TestNew"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TrialImageDownload", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("DownloadEndTime") + .HasColumnType("datetime2"); + + b.Property("DownloadStartTime") + .HasColumnType("datetime2"); + + b.Property("ImageCount") + .HasColumnType("int"); + + b.Property("ImageSize") + .HasColumnType("bigint"); + + b.Property("ImageType") + .HasColumnType("int"); + + b.Property("IsSuccess") + .HasColumnType("bit"); + + b.Property("SubjectVisitId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("TrialImageDownload"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", "ConfigType") + .WithMany() + .HasForeignKey("ConfigTypeId"); + + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", "Parent") + .WithMany("ChildList") + .HasForeignKey("ParentId"); + + b.Navigation("ConfigType"); + + b.Navigation("Parent"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", "Subject") + .WithMany("SubejectVisitList") + .HasForeignKey("SubjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Subject"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", "FinalSubjectVisit") + .WithMany() + .HasForeignKey("FinalSubjectVisitId"); + + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", "LatestSubjectVisit") + .WithMany() + .HasForeignKey("LatestSubjectVisitId"); + + b.Navigation("FinalSubjectVisit"); + + b.Navigation("LatestSubjectVisit"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestNew", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TrialImageDownload", null) + .WithMany("TestNewList") + .HasForeignKey("TrialImageDownloadId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", b => + { + b.Navigation("ChildList"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.Navigation("SubejectVisitList"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TrialImageDownload", b => + { + b.Navigation("TestNewList"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910075136_seftOneToManyAndOneToOne.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910075136_seftOneToManyAndOneToOne.cs new file mode 100644 index 000000000..32920762b --- /dev/null +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240910075136_seftOneToManyAndOneToOne.cs @@ -0,0 +1,60 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations +{ + /// + public partial class seftOneToManyAndOneToOne : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Dictionary", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + ParentId = table.Column(type: "uniqueidentifier", nullable: true), + ConfigTypeId = table.Column(type: "uniqueidentifier", nullable: true), + Code = table.Column(type: "nvarchar(max)", 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_Dictionary", x => x.Id); + table.ForeignKey( + name: "FK_Dictionary_Dictionary_ConfigTypeId", + column: x => x.ConfigTypeId, + principalTable: "Dictionary", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_Dictionary_Dictionary_ParentId", + column: x => x.ParentId, + principalTable: "Dictionary", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_Dictionary_ConfigTypeId", + table: "Dictionary", + column: "ConfigTypeId"); + + migrationBuilder.CreateIndex( + name: "IX_Dictionary_ParentId", + table: "Dictionary", + column: "ParentId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Dictionary"); + } + } +} diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs index f87ae145d..ffb12bafa 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs @@ -22,6 +22,105 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("Code") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ConfigTypeId") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("ParentId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("ConfigTypeId"); + + b.HasIndex("ParentId"); + + b.ToTable("Dictionary", (string)null); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("SubjectId") + .HasColumnType("uniqueidentifier"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("SubjectId"); + + b.ToTable("SubejectVisit", (string)null); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.Property("Id") + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("FinalSubjectVisitId") + .HasColumnType("uniqueidentifier"); + + b.Property("LatestSubjectVisitId") + .HasColumnType("uniqueidentifier"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("FinalSubjectVisitId"); + + b.HasIndex("LatestSubjectVisitId"); + + b.ToTable("Subject", (string)null); + }); + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestNew", b => { b.Property("Id") @@ -51,7 +150,7 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations b.HasIndex("TrialImageDownloadId"); - b.ToTable("TestNew"); + b.ToTable("TestNew", (string)null); }); modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TrialImageDownload", b => @@ -94,7 +193,48 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations b.HasKey("Id"); - b.ToTable("TrialImageDownload"); + b.ToTable("TrialImageDownload", (string)null); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", "ConfigType") + .WithMany() + .HasForeignKey("ConfigTypeId"); + + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", "Parent") + .WithMany("ChildList") + .HasForeignKey("ParentId"); + + b.Navigation("ConfigType"); + + b.Navigation("Parent"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", "Subject") + .WithMany("SubejectVisitList") + .HasForeignKey("SubjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Subject"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", "FinalSubjectVisit") + .WithMany() + .HasForeignKey("FinalSubjectVisitId"); + + b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.SubejectVisit", "LatestSubjectVisit") + .WithMany() + .HasForeignKey("LatestSubjectVisitId"); + + b.Navigation("FinalSubjectVisit"); + + b.Navigation("LatestSubjectVisit"); }); modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestNew", b => @@ -106,6 +246,16 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations .IsRequired(); }); + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Dictionary", b => + { + b.Navigation("ChildList"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Subject", b => + { + b.Navigation("SubejectVisitList"); + }); + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TrialImageDownload", b => { b.Navigation("TestNewList"); diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs index 06119c778..56a90d864 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs @@ -1,4 +1,5 @@ using IRaCIS.Core.Domain.Models; +using IRaCIS.Core.Domain.Share; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -48,7 +49,7 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL } - public class Subject: BaseFullAuditEntity + public class Subject : BaseFullAuditEntity { public string Name { get; set; } @@ -59,8 +60,10 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL #region 同时配置一对多,一对一 导航属性 + [ForeignKey("FinalSubjectVisitId")] public SubejectVisit FinalSubjectVisit { get; set; } + [ForeignKey("LatestSubjectVisitId")] public SubejectVisit LatestSubjectVisit { get; set; } public List SubejectVisitList { get; set; } @@ -68,11 +71,64 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL } - public class SubejectVisit: BaseFullAuditEntity + public class SubejectVisit : BaseFullAuditEntity { public Subject Subject { get; set; } - + public Guid SubjectId { get; set; } } + + + public class Dictionary : BaseFullAuditEntity + { + #region 导航属性 + [ForeignKey("ConfigTypeId")] + public Dictionary ConfigType { get; set; } + + [ForeignKey("ParentId")] + public Dictionary Parent { get; set; } + + public List ChildList { get; set; } = new List(); + + #endregion + + + public Guid? ParentId { get; set; } + + + public Guid? ConfigTypeId { get; set; } + + public string Code { get; set; } + } + + + public class TestOne : BaseFullAuditEntity + { + public string Code { get; set; } + + [ForeignKey(nameof(TestOne.FinalTestMany))] + public Guid? LatestTestManyId { get; set; } + + [ForeignKey(nameof(TestOne.LatestTestMany))] + public Guid? FinalTestManyId { get; set; } + + #region 同时配置一对多,一对一 导航属性 + + + public TestMany FinalTestMany { get; set; } + + public TestMany LatestTestMany { get; set; } + + public List TestManyList { get; set; } + + #endregion + } + + public class TestMany : BaseFullAuditEntity + { + public TestOne TestOne { get; set; } + + public Guid TestOneId { get; set; } + } }