修改计算列

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 int TotalMillisecondsInterval { get; set; }
public DateTime UploadStartTime { get; set; } public DateTime UploadStartTime { get; set; }

View File

@ -10,6 +10,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Reflection; using System.Reflection;
using EntityFramework.Exceptions.SqlServer; using EntityFramework.Exceptions.SqlServer;
using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infra.EFCore.ValueGenerator;
using MassTransit; using MassTransit;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata;
@ -20,7 +21,7 @@ namespace IRaCIS.Core.Infra.EFCore
{ {
public class IRaCISDBContext : DbContext public class IRaCISDBContext : DbContext
{ {
public readonly IUserInfo _userInfo; public readonly IUserInfo _userInfo;
// 在控制台 // 在控制台
//public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); //public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });
@ -66,13 +67,12 @@ namespace IRaCIS.Core.Infra.EFCore
#endregion #endregion
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
//modelBuilder.Entity<User>().Property(t => t.FullName) .HasDefaultValueSql("[LastName] + ' / ' + [FirstName]"); //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) if (_userInfo.IsEn_Us)
{ {
modelBuilder.Entity<Dictionary>().Property(t => t.MappedValue).HasColumnName(nameof(Domain.Models.Dictionary.Value)); modelBuilder.Entity<Dictionary>().Property(t => t.MappedValue).HasColumnName(nameof(Domain.Models.Dictionary.Value));
@ -350,7 +350,7 @@ namespace IRaCIS.Core.Infra.EFCore
updateEntity1.UpdateUserId = _userInfo.Id; updateEntity1.UpdateUserId = _userInfo.Id;
} }
if(t.Entity is ISoftDelete updateEntity2) if (t.Entity is ISoftDelete updateEntity2)
{ {
if (updateEntity2.IsDeleted == true) if (updateEntity2.IsDeleted == true)
{ {

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.Domain.Models;
using IRaCIS.Core.Infra.EFCore.ValueGenerator;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
@ -11,11 +12,13 @@ namespace IRaCIS.Core.Infra.EFCore.EntityConfigration
public void Configure(EntityTypeBuilder<StudyMonitor> builder) public void Configure(EntityTypeBuilder<StudyMonitor> builder)
{ {
// builder // builder
// .HasMany(s => s.TrialSiteUserList) // .HasMany(s => s.TrialSiteUserList)
//.WithOne(c => c.DicomStudy) //.WithOne(c => c.DicomStudy)
//.HasForeignKey(s => new { s.TrialId, s.SiteId }) //.HasForeignKey(s => new { s.TrialId, s.SiteId })
//.HasPrincipalKey(c => new { c.TrialId, c.SiteId }); //.HasPrincipalKey(c => new { c.TrialId, c.SiteId });
builder.Property(e => e.TotalMillisecondsInterval).HasValueGenerator<UploadTotalMillisecondsInterval>().ValueGeneratedOnAdd();
builder builder