diff --git a/IRaCIS.Core.Domain/Image/DicomStudyMonitor.cs b/IRaCIS.Core.Domain/Image/DicomStudyMonitor.cs index 24305ec5e..2616aa23b 100644 --- a/IRaCIS.Core.Domain/Image/DicomStudyMonitor.cs +++ b/IRaCIS.Core.Domain/Image/DicomStudyMonitor.cs @@ -32,6 +32,7 @@ namespace IRaCIS.Core.Domain.Models public int TotalMillisecondsInterval { get; set; } + public DateTime UploadStartTime { get; set; } diff --git a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs index 59ddd119e..c0a148cd4 100644 --- a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs +++ b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs @@ -10,6 +10,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking; using System.Reflection; using EntityFramework.Exceptions.SqlServer; using IRaCIS.Core.Domain.Share; +using IRaCIS.Core.Infra.EFCore.ValueGenerator; using MassTransit; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; @@ -20,7 +21,7 @@ namespace IRaCIS.Core.Infra.EFCore { public class IRaCISDBContext : DbContext { - public readonly IUserInfo _userInfo; + public readonly IUserInfo _userInfo; // 在控制台 //public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); @@ -33,7 +34,7 @@ namespace IRaCIS.Core.Infra.EFCore //_configuration = configuration; } - + //比数据库上下文构造函数先执行 不能构造函数注入的方式使用配置文件 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -66,13 +67,12 @@ namespace IRaCIS.Core.Infra.EFCore #endregion + + protected override void OnModelCreating(ModelBuilder modelBuilder) { //modelBuilder.Entity().Property(t => t.FullName) .HasDefaultValueSql("[LastName] + ' / ' + [FirstName]"); - //code first must migration dbfirst must config in db - modelBuilder.Entity().Property(e => e.TotalMillisecondsInterval).HasComputedColumnSql("datediff(MS,UploadStartTime,UploadFinishedTime)"); - if (_userInfo.IsEn_Us) { modelBuilder.Entity().Property(t => t.MappedValue).HasColumnName(nameof(Domain.Models.Dictionary.Value)); @@ -238,7 +238,7 @@ namespace IRaCIS.Core.Infra.EFCore #region QA - + public virtual DbSet QCChallengeDialog { get; set; } public virtual DbSet QATemplateDictionary { get; set; } public virtual DbSet QCChallenge { get; set; } @@ -328,7 +328,7 @@ namespace IRaCIS.Core.Infra.EFCore #region 重写savechange方式 统一增加审计信息 CreateUserId CreateTime UpdateTime UpdateUserId 可用事件绑定的方式UpdateAuitUser private void UpdateAuditInfo() { - + ChangeTracker.DetectChanges(); // Important! //// 获取所有更改,删除,新增的实体,但排除审计实体(避免死循环) @@ -350,7 +350,7 @@ namespace IRaCIS.Core.Infra.EFCore updateEntity1.UpdateUserId = _userInfo.Id; } - if(t.Entity is ISoftDelete updateEntity2) + if (t.Entity is ISoftDelete updateEntity2) { if (updateEntity2.IsDeleted == true) { @@ -374,7 +374,7 @@ namespace IRaCIS.Core.Infra.EFCore { addEntity.CreateTime = DateTime.UtcNow.AddHours(8); } - + addEntity.CreateUserId = _userInfo.Id; } @@ -395,7 +395,7 @@ namespace IRaCIS.Core.Infra.EFCore } - + } #endregion diff --git a/IRaCIS.Core.Infra.EFCore/Context/ValueGenerator/UploadTotalMillisecondsIntervalGenerator.cs b/IRaCIS.Core.Infra.EFCore/Context/ValueGenerator/UploadTotalMillisecondsIntervalGenerator.cs new file mode 100644 index 000000000..5c2aba700 --- /dev/null +++ b/IRaCIS.Core.Infra.EFCore/Context/ValueGenerator/UploadTotalMillisecondsIntervalGenerator.cs @@ -0,0 +1,31 @@ +using System; +using IRaCIS.Core.Domain.Models; +using Microsoft.EntityFrameworkCore.ChangeTracking; +using Microsoft.EntityFrameworkCore.ValueGeneration; + +namespace IRaCIS.Core.Infra.EFCore.ValueGenerator +{ + public class UploadTotalMillisecondsInterval : ValueGenerator + { + + //code first must migration dbfirst must config in db and also need config in code + //modelBuilder.Entity().Property(e => e.TotalMillisecondsInterval).HasComputedColumnSql("datediff(MS,UploadStartTime,UploadFinishedTime)"); + + //modelBuilder.Entity().Property(e => e.TestInterval).ValueGeneratedOnAddOrUpdate().HasDefaultValueSql("datediff(MS,UploadStartTime,UploadFinishedTime)"); + + + public override int Next(EntityEntry entry) + { + if (entry.Entity is StudyMonitor entity) + { + return (entity.UploadFinishedTime - entity.UploadStartTime).Milliseconds; + } + else + { + throw new ArgumentException("ValueGenerator用在了不适合的实体"); + + } + } + public override bool GeneratesTemporaryValues => false; + } +} \ No newline at end of file diff --git a/IRaCIS.Core.Infra.EFCore/EntityConfigration/StudyMonitorConfigration.cs b/IRaCIS.Core.Infra.EFCore/EntityConfigration/StudyMonitorConfigration.cs index dfd300a82..142e4a78e 100644 --- a/IRaCIS.Core.Infra.EFCore/EntityConfigration/StudyMonitorConfigration.cs +++ b/IRaCIS.Core.Infra.EFCore/EntityConfigration/StudyMonitorConfigration.cs @@ -1,4 +1,5 @@ using IRaCIS.Core.Domain.Models; +using IRaCIS.Core.Infra.EFCore.ValueGenerator; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; @@ -11,11 +12,13 @@ namespace IRaCIS.Core.Infra.EFCore.EntityConfigration public void Configure(EntityTypeBuilder builder) { - // builder - // .HasMany(s => s.TrialSiteUserList) - //.WithOne(c => c.DicomStudy) - //.HasForeignKey(s => new { s.TrialId, s.SiteId }) - //.HasPrincipalKey(c => new { c.TrialId, c.SiteId }); + // builder + // .HasMany(s => s.TrialSiteUserList) + //.WithOne(c => c.DicomStudy) + //.HasForeignKey(s => new { s.TrialId, s.SiteId }) + //.HasPrincipalKey(c => new { c.TrialId, c.SiteId }); + + builder.Property(e => e.TotalMillisecondsInterval).HasValueGenerator().ValueGeneratedOnAdd(); builder