修改计算列

Uat_Study
hang 2022-04-21 15:01:53 +08:00
parent fca6be727c
commit 6d5f73a415
4 changed files with 50 additions and 15 deletions

View File

@ -32,6 +32,7 @@ namespace IRaCIS.Core.Domain.Models
public int TotalMillisecondsInterval { get; set; }
public DateTime UploadStartTime { get; set; }

View File

@ -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<User>().Property(t => t.FullName) .HasDefaultValueSql("[LastName] + ' / ' + [FirstName]");
//code first must migration dbfirst must config in db
modelBuilder.Entity<StudyMonitor>().Property(e => e.TotalMillisecondsInterval).HasComputedColumnSql("datediff(MS,UploadStartTime,UploadFinishedTime)");
if (_userInfo.IsEn_Us)
{
modelBuilder.Entity<Dictionary>().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> QCChallengeDialog { get; set; }
public virtual DbSet<QANotice> QATemplateDictionary { get; set; }
public virtual DbSet<QCChallenge> 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

View File

@ -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<int>
{
//code first must migration dbfirst must config in db and also need config in code
//modelBuilder.Entity<StudyMonitor>().Property(e => e.TotalMillisecondsInterval).HasComputedColumnSql("datediff(MS,UploadStartTime,UploadFinishedTime)");
//modelBuilder.Entity<StudyMonitor>().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;
}
}

View File

@ -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<StudyMonitor> 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<UploadTotalMillisecondsInterval>().ValueGeneratedOnAdd();
builder