From b0fa7082710216c5138f96cfe9b3c691a51cee91 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Thu, 21 Apr 2022 13:48:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E6=8E=89=E8=AE=B0=E5=BD=95=E5=AE=A1?= =?UTF-8?q?=E8=AE=A1=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs | 2 +- .../Service/Document/TrialDocumentService.cs | 2 ++ .../Service/TrialSiteUser/TrialService.cs | 2 +- .../Service/Visit/VisitPlanService.cs | 7 +++-- IRaCIS.Core.Domain/BaseModel/Entity.cs | 18 ++--------- .../Context/IRaCISDBContext.cs | 30 ++++++++++++------- 6 files changed, 31 insertions(+), 30 deletions(-) diff --git a/IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs index c0002f379..eac408ead 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/EFSetup.cs @@ -24,7 +24,7 @@ namespace IRaCIS.Core.API options.UseProjectables(); - options.AddInterceptors(new AuditingInterceptor(configuration.GetSection("ConnectionStrings:RemoteNew").Value)); + //options.AddInterceptors(new AuditingInterceptor(configuration.GetSection("ConnectionStrings:RemoteNew").Value)); }); } diff --git a/IRaCIS.Core.Application/Service/Document/TrialDocumentService.cs b/IRaCIS.Core.Application/Service/Document/TrialDocumentService.cs index bb956a6fc..563e41a40 100644 --- a/IRaCIS.Core.Application/Service/Document/TrialDocumentService.cs +++ b/IRaCIS.Core.Application/Service/Document/TrialDocumentService.cs @@ -10,6 +10,7 @@ using IRaCIS.Core.Infra.EFCore; using Microsoft.AspNetCore.Hosting; using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Domain.Share; +using MassTransit; using Panda.DynamicWebApi.Attributes; namespace IRaCIS.Core.Application.Services @@ -406,6 +407,7 @@ namespace IRaCIS.Core.Application.Services return ResponseOutput.NotOk("同类型已存在该文件名"); } + //entity.Id = NewId.NextGuid(); await _repository.AddAsync(entity, true); return ResponseOutput.Ok(entity.Id.ToString()); } diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialService.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialService.cs index b8080284a..5ea307176 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialService.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialService.cs @@ -152,7 +152,7 @@ namespace IRaCIS.Application.Services //var test = _trialRepository.Where(t => t.CreateTime.Year == DateTime.Now.Year + 1).Select(t => t.Code).DefaultIfEmpty(1).ToList(); var trial = _mapper.Map(trialAddModel); - trial.Id = NewId.NextGuid(); + //trial.Id = NewId.NextGuid(); var yearStr = DateTime.Now.Year.ToString(); trial.Code = currentYearMaxCodeNext; diff --git a/IRaCIS.Core.Application/Service/Visit/VisitPlanService.cs b/IRaCIS.Core.Application/Service/Visit/VisitPlanService.cs index 66b33faf6..5b55fcf00 100644 --- a/IRaCIS.Core.Application/Service/Visit/VisitPlanService.cs +++ b/IRaCIS.Core.Application/Service/Visit/VisitPlanService.cs @@ -155,7 +155,7 @@ namespace IRaCIS.Application.Services svItem.SiteId = subject.SiteId; svItem.SubjectId = subject.SubjectId; - svItem.Id = Guid.NewGuid(); + svItem.Id = NewId.NextGuid(); //设置了末次访视,那么加访视计划的时候,设置为不可用 if (subjectSVS.Any(t => t.SubjectId == svItem.SubjectId && t.IsFinalVisit)) @@ -174,9 +174,10 @@ namespace IRaCIS.Application.Services visitPlanItem.BlindName = "B" + ((int)visitPlanItem.VisitNum * 10).ToString("D3"); + //var result = await _visitStageRepository.AddAsync(visitPlanItem); + var result = await _repository.AddAsync(visitPlanItem); - visitPlan.Id = result.Id; - + //更新项目访视计划状态为未确认 await _trialRepository.BatchUpdateAsync(u => u.Id == visitPlan.TrialId, t => new Trial() { VisitPlanConfirmed = false }); diff --git a/IRaCIS.Core.Domain/BaseModel/Entity.cs b/IRaCIS.Core.Domain/BaseModel/Entity.cs index d5477d8ca..939bf9f13 100644 --- a/IRaCIS.Core.Domain/BaseModel/Entity.cs +++ b/IRaCIS.Core.Domain/BaseModel/Entity.cs @@ -1,32 +1,20 @@ using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using MassTransit; namespace IRaCIS.Core.Domain.Models { - public abstract class Entity : IEntity + public abstract class Entity : IEntity { [Key] [Required] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public Guid Id { get ; set ; } + [DatabaseGenerated(DatabaseGeneratedOption.None)] + public Guid Id { get; set; } } public interface IEntity { - /// - /// 编号 - /// - - - abstract TKey Id { get; set; } } - - //public class Entity : IEntity - //{ - - //} } diff --git a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs index b0a6369bc..5a5dc96f5 100644 --- a/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs +++ b/IRaCIS.Core.Infra.EFCore/Context/IRaCISDBContext.cs @@ -13,6 +13,7 @@ using IRaCIS.Core.Domain.Share; using MassTransit; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.ValueGeneration; using UserTypeGroup = IRaCIS.Core.Domain.Models.UserTypeGroup; namespace IRaCIS.Core.Infra.EFCore @@ -67,7 +68,10 @@ namespace IRaCIS.Core.Infra.EFCore 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) { @@ -79,9 +83,6 @@ namespace IRaCIS.Core.Infra.EFCore } - //modelBuilder.Entity().Property(t => t.FullName) .HasDefaultValueSql("[LastName] + ' / ' + [FirstName]"); - - //遍历实体模型手动配置 var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null); foreach (var type in typesToRegister) @@ -92,17 +93,19 @@ namespace IRaCIS.Core.Infra.EFCore base.OnModelCreating(modelBuilder); - //code first must migration dbfirst must config in db - modelBuilder.Entity().Property(e => e.TotalMillisecondsInterval).HasComputedColumnSql("datediff(MS,UploadStartTime,UploadFinishedTime)"); - - // 软删除配置 foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { + // 软删除配置 if (typeof(ISoftDelete).IsAssignableFrom(entityType.ClrType)) { entityType.AddSoftDeleteQueryFilter(); } + + if (typeof(Entity).IsAssignableFrom(entityType.ClrType)) + { + modelBuilder.Entity(entityType.ClrType).Property("Id").HasValueGenerator(); + } } } @@ -310,8 +313,6 @@ namespace IRaCIS.Core.Infra.EFCore public virtual DbSet CommonDocument { get; set; } - - public override int SaveChanges() { UpdateAuditInfo(); @@ -361,7 +362,7 @@ namespace IRaCIS.Core.Infra.EFCore //添加的时候,更新审计字段也赋值 case EntityState.Added: - //// 仓储添加时 就有id了 + ////// 仓储添加时 就有id了 //if (t.Entity is Entity entity && entity.Id == Guid.Empty) //{ // entity.Id = NewId.NextGuid(); @@ -453,4 +454,13 @@ namespace IRaCIS.Core.Infra.EFCore #endregion } + + public class MySequentialGuidValueGenerator : ValueGenerator + { + public override Guid Next(EntityEntry entry) + { + return NewId.NextGuid(); + } + public override bool GeneratesTemporaryValues => false; + } } \ No newline at end of file