1616using SmartStore . Core . Data ;
1717using SmartStore . Core . Data . Hooks ;
1818using SmartStore . Core . Events ;
19+ using System . Data . Entity . Core . Objects ;
1920
2021namespace SmartStore . Data
2122{
@@ -138,7 +139,7 @@ public bool HooksEnabled
138139
139140 public virtual string CreateDatabaseScript ( )
140141 {
141- return ( ( IObjectContextAdapter ) this ) . ObjectContext . CreateDatabaseScript ( ) ;
142+ return ObjectContext . CreateDatabaseScript ( ) ;
142143 }
143144
144145 public new DbSet < TEntity > Set < TEntity > ( ) where TEntity : BaseEntity
@@ -219,7 +220,7 @@ private IEnumerable<DbParameter> ToParameters(params object[] parameters)
219220
220221 // database call
221222 var reader = cmd . ExecuteReader ( ) ;
222- var result = ( ( IObjectContextAdapter ) ( this ) ) . ObjectContext . Translate < TEntity > ( reader ) . ToList ( ) ;
223+ var result = ObjectContext . Translate < TEntity > ( reader ) . ToList ( ) ;
223224 if ( ! ForceNoTracking )
224225 {
225226 for ( int i = 0 ; i < result . Count ; i ++ )
@@ -263,8 +264,8 @@ public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, in
263264 if ( timeout . HasValue )
264265 {
265266 //store previous timeout
266- previousTimeout = ( ( IObjectContextAdapter ) this ) . ObjectContext . CommandTimeout ;
267- ( ( IObjectContextAdapter ) this ) . ObjectContext . CommandTimeout = timeout ;
267+ previousTimeout = ObjectContext . CommandTimeout ;
268+ ObjectContext . CommandTimeout = timeout ;
268269 }
269270
270271 var transactionalBehavior = doNotEnsureTransaction
@@ -275,7 +276,7 @@ public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, in
275276 if ( timeout . HasValue )
276277 {
277278 //Set previous timeout back
278- ( ( IObjectContextAdapter ) this ) . ObjectContext . CommandTimeout = previousTimeout ;
279+ ObjectContext . CommandTimeout = previousTimeout ;
279280 }
280281
281282 return result ;
@@ -498,27 +499,29 @@ protected internal bool IsSqlServer2012OrHigher()
498499
499500 public TEntity Attach < TEntity > ( TEntity entity ) where TEntity : BaseEntity
500501 {
501- var dbSet = Set < TEntity > ( ) ;
502- var alreadyAttached = dbSet . Local . FirstOrDefault ( x => x . Id == entity . Id ) ;
503-
504- if ( alreadyAttached == null )
502+ ObjectStateEntry entry ;
503+ if ( ObjectContext . ObjectStateManager . TryGetObjectStateEntry ( entity , out entry ) )
505504 {
506- dbSet . Attach ( entity ) ;
507- return entity ;
505+ if ( entry . State != System . Data . Entity . EntityState . Detached )
506+ {
507+ return ( TEntity ) entry . Entity ;
508+ }
508509 }
509510
510- return alreadyAttached ;
511+ Set < TEntity > ( ) . Attach ( entity ) ;
512+ return entity ;
511513 }
512514
513515 public bool IsAttached < TEntity > ( TEntity entity ) where TEntity : BaseEntity
514516 {
515- if ( entity != null )
517+ ObjectStateEntry entry ;
518+ if ( ObjectContext . ObjectStateManager . TryGetObjectStateEntry ( entity , out entry ) )
516519 {
517- return Set < TEntity > ( ) . Local . Any ( x => x . Id == entity . Id ) ;
520+ return ( entry . State != System . Data . Entity . EntityState . Detached ) ;
518521 }
519-
522+
520523 return false ;
521- }
524+ }
522525
523526 public void DetachEntity < TEntity > ( TEntity entity ) where TEntity : BaseEntity
524527 {
@@ -550,7 +553,6 @@ public int DetachEntities<TEntity>(bool unchangedEntitiesOnly = true) where TEnt
550553
551554 public void ChangeState < TEntity > ( TEntity entity , System . Data . Entity . EntityState newState ) where TEntity : BaseEntity
552555 {
553- Console . WriteLine ( "ChangeState ORIGINAL" ) ;
554556 this . Entry ( entity ) . State = newState ;
555557 }
556558
@@ -559,6 +561,11 @@ public void ReloadEntity<TEntity>(TEntity entity) where TEntity : BaseEntity
559561 this . Entry ( entity ) . Reload ( ) ;
560562 }
561563
564+ internal ObjectContext ObjectContext
565+ {
566+ get { return ( ( IObjectContextAdapter ) this ) . ObjectContext ; }
567+ }
568+
562569 private string FormatValidationExceptionMessage ( IEnumerable < DbEntityValidationResult > results )
563570 {
564571 var sb = new StringBuilder ( ) ;
0 commit comments