@@ -31,9 +31,9 @@ public class EfUnitOfWork : UnitOfWorkBase, ITransientDependency
31
31
/// Creates a new <see cref="EfUnitOfWork"/>.
32
32
/// </summary>
33
33
public EfUnitOfWork (
34
- IIocResolver iocResolver ,
35
- IConnectionStringResolver connectionStringResolver ,
36
- IDbContextResolver dbContextResolver ,
34
+ IIocResolver iocResolver ,
35
+ IConnectionStringResolver connectionStringResolver ,
36
+ IDbContextResolver dbContextResolver ,
37
37
IUnitOfWorkDefaultOptions defaultOptions )
38
38
: base ( connectionStringResolver , defaultOptions )
39
39
{
@@ -137,14 +137,18 @@ public virtual TDbContext GetOrCreateDbContext<TDbContext>(MultiTenancySides? mu
137
137
connectionStringResolveArgs [ "DbContextType" ] = typeof ( TDbContext ) ;
138
138
var connectionString = ResolveConnectionString ( connectionStringResolveArgs ) ;
139
139
140
- var dbContextKey = typeof ( TDbContext ) . FullName + "#" + connectionString ;
140
+ var dbContextKey = typeof ( TDbContext ) . FullName + "#" + connectionString ;
141
141
142
142
DbContext dbContext ;
143
143
if ( ! ActiveDbContexts . TryGetValue ( dbContextKey , out dbContext ) )
144
144
{
145
145
146
146
dbContext = _dbContextResolver . Resolve < TDbContext > ( connectionString ) ;
147
- ( ( IObjectContextAdapter ) dbContext ) . ObjectContext . ObjectMaterialized += ObjectContext_ObjectMaterialized ;
147
+
148
+ ( ( IObjectContextAdapter ) dbContext ) . ObjectContext . ObjectMaterialized += ( sender , args ) =>
149
+ {
150
+ ObjectContext_ObjectMaterialized ( dbContext , args ) ;
151
+ } ;
148
152
149
153
foreach ( var filter in Filters )
150
154
{
@@ -197,17 +201,21 @@ protected virtual async Task SaveChangesInDbContextAsync(DbContext dbContext)
197
201
{
198
202
await dbContext . SaveChangesAsync ( ) ;
199
203
}
200
-
204
+
201
205
protected virtual void Release ( DbContext dbContext )
202
206
{
203
207
dbContext . Dispose ( ) ;
204
208
IocResolver . Release ( dbContext ) ;
205
209
}
206
210
207
- private static void ObjectContext_ObjectMaterialized ( object sender , ObjectMaterializedEventArgs e )
211
+ private static void ObjectContext_ObjectMaterialized ( DbContext dbContext , ObjectMaterializedEventArgs e )
208
212
{
209
213
var entityType = ObjectContext . GetObjectType ( e . Entity . GetType ( ) ) ;
210
- DateTimePropertyInfoHelper . NormalizeDatePropertyKinds ( e . Entity , entityType ) ;
214
+ var previousState = dbContext . Entry ( e . Entity ) . State ;
215
+
216
+ DateTimePropertyInfoHelper . NormalizeDatePropertyKinds ( e . Entity , entityType ) ;
217
+
218
+ dbContext . Entry ( e . Entity ) . State = previousState ;
211
219
}
212
220
}
213
221
}
0 commit comments