1616using SmartStore . Core . Data ;
1717using SmartStore . Core . Data . Hooks ;
1818using SmartStore . Core . Events ;
19- using System . Data . Entity . Core . Objects ;
2019
2120namespace SmartStore . Data
2221{
@@ -139,7 +138,7 @@ public bool HooksEnabled
139138
140139 public virtual string CreateDatabaseScript ( )
141140 {
142- return ObjectContext . CreateDatabaseScript ( ) ;
141+ return ( ( IObjectContextAdapter ) this ) . ObjectContext . CreateDatabaseScript ( ) ;
143142 }
144143
145144 public new DbSet < TEntity > Set < TEntity > ( ) where TEntity : BaseEntity
@@ -220,7 +219,7 @@ private IEnumerable<DbParameter> ToParameters(params object[] parameters)
220219
221220 // database call
222221 var reader = cmd . ExecuteReader ( ) ;
223- var result = ObjectContext . Translate < TEntity > ( reader ) . ToList ( ) ;
222+ var result = ( ( IObjectContextAdapter ) ( this ) ) . ObjectContext . Translate < TEntity > ( reader ) . ToList ( ) ;
224223 if ( ! ForceNoTracking )
225224 {
226225 for ( int i = 0 ; i < result . Count ; i ++ )
@@ -264,8 +263,8 @@ public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, in
264263 if ( timeout . HasValue )
265264 {
266265 //store previous timeout
267- previousTimeout = ObjectContext . CommandTimeout ;
268- ObjectContext . CommandTimeout = timeout ;
266+ previousTimeout = ( ( IObjectContextAdapter ) this ) . ObjectContext . CommandTimeout ;
267+ ( ( IObjectContextAdapter ) this ) . ObjectContext . CommandTimeout = timeout ;
269268 }
270269
271270 var transactionalBehavior = doNotEnsureTransaction
@@ -276,7 +275,7 @@ public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, in
276275 if ( timeout . HasValue )
277276 {
278277 //Set previous timeout back
279- ObjectContext . CommandTimeout = previousTimeout ;
278+ ( ( IObjectContextAdapter ) this ) . ObjectContext . CommandTimeout = previousTimeout ;
280279 }
281280
282281 return result ;
@@ -499,29 +498,27 @@ protected internal bool IsSqlServer2012OrHigher()
499498
500499 public TEntity Attach < TEntity > ( TEntity entity ) where TEntity : BaseEntity
501500 {
502- ObjectStateEntry entry ;
503- if ( ObjectContext . ObjectStateManager . TryGetObjectStateEntry ( entity , out entry ) )
501+ var dbSet = Set < TEntity > ( ) ;
502+ var alreadyAttached = dbSet . Local . FirstOrDefault ( x => x . Id == entity . Id ) ;
503+
504+ if ( alreadyAttached == null )
504505 {
505- if ( entry . State != System . Data . Entity . EntityState . Detached )
506- {
507- return ( TEntity ) entry . Entity ;
508- }
506+ dbSet . Attach ( entity ) ;
507+ return entity ;
509508 }
510509
511- Set < TEntity > ( ) . Attach ( entity ) ;
512- return entity ;
510+ return alreadyAttached ;
513511 }
514512
515513 public bool IsAttached < TEntity > ( TEntity entity ) where TEntity : BaseEntity
516514 {
517- ObjectStateEntry entry ;
518- if ( ObjectContext . ObjectStateManager . TryGetObjectStateEntry ( entity , out entry ) )
515+ if ( entity != null )
519516 {
520- return ( entry . State != System . Data . Entity . EntityState . Detached ) ;
517+ return Set < TEntity > ( ) . Local . Any ( x => x . Id == entity . Id ) ;
521518 }
522-
519+
523520 return false ;
524- }
521+ }
525522
526523 public void DetachEntity < TEntity > ( TEntity entity ) where TEntity : BaseEntity
527524 {
@@ -553,6 +550,7 @@ public int DetachEntities<TEntity>(bool unchangedEntitiesOnly = true) where TEnt
553550
554551 public void ChangeState < TEntity > ( TEntity entity , System . Data . Entity . EntityState newState ) where TEntity : BaseEntity
555552 {
553+ Console . WriteLine ( "ChangeState ORIGINAL" ) ;
556554 this . Entry ( entity ) . State = newState ;
557555 }
558556
@@ -561,11 +559,6 @@ public void ReloadEntity<TEntity>(TEntity entity) where TEntity : BaseEntity
561559 this . Entry ( entity ) . Reload ( ) ;
562560 }
563561
564- internal ObjectContext ObjectContext
565- {
566- get { return ( ( IObjectContextAdapter ) this ) . ObjectContext ; }
567- }
568-
569562 private string FormatValidationExceptionMessage ( IEnumerable < DbEntityValidationResult > results )
570563 {
571564 var sb = new StringBuilder ( ) ;
0 commit comments