1
- // -----------------------------------------------------------------------
1
+ // -----------------------------------------------------------------------
2
2
// <copyright file="DbContextBase.cs" company="OSharp开源团队">
3
3
// Copyright (c) 2014-2019 OSharp. All rights reserved.
4
4
// </copyright>
@@ -45,7 +45,7 @@ protected DbContextBase(DbContextOptions options, IServiceProvider serviceProvid
45
45
_serviceProvider = serviceProvider ;
46
46
_entityManager = serviceProvider . GetService < IEntityManager > ( ) ;
47
47
_osharpDbOptions = serviceProvider ? . GetOSharpOptions ( ) ? . DbContexts ? . Values . FirstOrDefault ( m => m . DbContextType == GetType ( ) ) ;
48
- Logger = serviceProvider ? . GetLogger ( this ) ;
48
+ Logger = serviceProvider . GetLogger ( this ) ;
49
49
}
50
50
51
51
/// <summary>
@@ -80,10 +80,10 @@ protected DbContextBase(DbContextOptions options, IServiceProvider serviceProvid
80
80
public override int SaveChanges ( )
81
81
{
82
82
IList < AuditEntityEntry > auditEntities = new List < AuditEntityEntry > ( ) ;
83
- if ( _osharpDbOptions ? . AuditEntityEnabled == true )
83
+ if ( _osharpDbOptions . AuditEntityEnabled == true )
84
84
{
85
85
IAuditEntityProvider auditEntityProvider = _serviceProvider . GetService < IAuditEntityProvider > ( ) ;
86
- auditEntities = auditEntityProvider ? . GetAuditEntities ( this ) ? . ToList ( ) ;
86
+ auditEntities = auditEntityProvider ? . GetAuditEntities ( this ) . ToList ( ) ;
87
87
}
88
88
89
89
//开启或使用现有事务
@@ -94,7 +94,7 @@ public override int SaveChanges()
94
94
{
95
95
AuditEntityEventData eventData = new AuditEntityEventData ( auditEntities ) ;
96
96
IEventBus eventBus = _serviceProvider . GetService < IEventBus > ( ) ;
97
- eventBus ? . Publish ( this , eventData ) ;
97
+ eventBus . Publish ( this , eventData ) ;
98
98
}
99
99
100
100
return count ;
@@ -128,10 +128,10 @@ public override int SaveChanges()
128
128
public override async Task < int > SaveChangesAsync ( CancellationToken cancellationToken = new CancellationToken ( ) )
129
129
{
130
130
IList < AuditEntityEntry > auditEntities = new List < AuditEntityEntry > ( ) ;
131
- if ( _osharpDbOptions ? . AuditEntityEnabled == true )
131
+ if ( _osharpDbOptions . AuditEntityEnabled == true )
132
132
{
133
133
IAuditEntityProvider auditEntityProvider = _serviceProvider . GetService < IAuditEntityProvider > ( ) ;
134
- auditEntities = auditEntityProvider ? . GetAuditEntities ( this ) ? . ToList ( ) ;
134
+ auditEntities = auditEntityProvider ? . GetAuditEntities ( this ) . ToList ( ) ;
135
135
}
136
136
137
137
//开启或使用现有事务
@@ -189,20 +189,26 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
189
189
foreach ( IEntityRegister register in registers )
190
190
{
191
191
register . RegisterTo ( modelBuilder ) ;
192
- Logger ? . LogDebug ( $ "将实体类 { register . EntityType } 注册到上下文 { contextType } 中") ;
192
+ Logger . LogDebug ( $ "将实体类 { register . EntityType } 注册到上下文 { contextType } 中") ;
193
193
}
194
- Logger ? . LogInformation ( $ "上下文 { contextType } 注册了{ registers . Length } 个实体类") ;
194
+ Logger . LogInformation ( $ "上下文 { contextType } 注册了{ registers . Length } 个实体类") ;
195
195
196
- //按预定前缀更改表名
197
- var entityTypes = modelBuilder . Model . GetEntityTypes ( ) . ToList ( ) ;
196
+ List < IMutableEntityType > entityTypes = modelBuilder . Model . GetEntityTypes ( ) . ToList ( ) ;
198
197
foreach ( IMutableEntityType entityType in entityTypes )
199
198
{
199
+ //启用时间属性UTC格式
200
+ if ( _osharpDbOptions . DateTimeUtcFormatEnabled )
201
+ {
202
+ IEntityDateTimeUtcConversion utcConversion = _serviceProvider . GetService < IEntityDateTimeUtcConversion > ( ) ;
203
+ utcConversion . Convert ( entityType ) ;
204
+ }
205
+
206
+ //按预定前缀更改表名
200
207
string prefix = GetTableNamePrefix ( entityType . ClrType ) ;
201
208
if ( prefix . IsNullOrEmpty ( ) )
202
209
{
203
210
continue ;
204
211
}
205
-
206
212
modelBuilder . Entity ( entityType . ClrType ) . ToTable ( $ "{ prefix } _{ entityType . GetTableName ( ) } ") ;
207
213
}
208
214
}
0 commit comments