88using Microsoft . EntityFrameworkCore . Metadata . Builders ;
99using Microsoft . EntityFrameworkCore . Query ;
1010using Microsoft . EntityFrameworkCore . Query . Internal ;
11+ using Microsoft . EntityFrameworkCore . Storage ;
1112using Microsoft . Extensions . Configuration ;
1213using System ;
1314using System . CodeDom . Compiler ;
@@ -1816,7 +1817,7 @@ public class FakeTestDbContext : ITestDbContext
18161817
18171818 public FakeTestDbContext ( )
18181819 {
1819- _database = null ;
1820+ _database = new FakeDatabaseFacade ( new TestDbContext ( ) ) ;
18201821
18211822 A = new FakeDbSet < A > ( "AId" ) ;
18221823 Aarefs = new FakeDbSet < Aaref > ( "C1" , "C2" ) ;
@@ -2830,16 +2831,23 @@ public override EntityEntry<TEntity> Add(TEntity entity)
28302831 return null ;
28312832 }
28322833
2834+ public override ValueTask < EntityEntry < TEntity > > AddAsync ( TEntity entity , CancellationToken cancellationToken = default )
2835+ {
2836+ return new ValueTask < EntityEntry < TEntity > > ( Task < EntityEntry < TEntity > > . Factory . StartNew ( ( ) => Add ( entity ) , cancellationToken ) ) ;
2837+ }
2838+
28332839 public override void AddRange ( params TEntity [ ] entities )
28342840 {
28352841 if ( entities == null ) throw new ArgumentNullException ( "entities" ) ;
2836- foreach ( var entity in entities . ToList ( ) )
2842+ foreach ( var entity in entities )
28372843 _data . Add ( entity ) ;
28382844 }
28392845
28402846 public override void AddRange ( IEnumerable < TEntity > entities )
28412847 {
2842- AddRange ( entities . ToArray ( ) ) ;
2848+ if ( entities == null ) throw new ArgumentNullException ( "entities" ) ;
2849+ foreach ( var entity in entities )
2850+ _data . Add ( entity ) ;
28432851 }
28442852
28452853 public override Task AddRangeAsync ( params TEntity [ ] entities )
@@ -2848,12 +2856,36 @@ public override Task AddRangeAsync(params TEntity[] entities)
28482856 return Task . Factory . StartNew ( ( ) => AddRange ( entities ) ) ;
28492857 }
28502858
2859+ public override Task AddRangeAsync ( IEnumerable < TEntity > entities , CancellationToken cancellationToken = default )
2860+ {
2861+ if ( entities == null ) throw new ArgumentNullException ( "entities" ) ;
2862+ return Task . Factory . StartNew ( ( ) => AddRange ( entities ) , cancellationToken ) ;
2863+ }
2864+
2865+ public override EntityEntry < TEntity > Attach ( TEntity entity )
2866+ {
2867+ if ( entity == null ) throw new ArgumentNullException ( "entity" ) ;
2868+ return Add ( entity ) ;
2869+ }
2870+
28512871 public override void AttachRange ( params TEntity [ ] entities )
28522872 {
28532873 if ( entities == null ) throw new ArgumentNullException ( "entities" ) ;
28542874 AddRange ( entities ) ;
28552875 }
28562876
2877+ public override void AttachRange ( IEnumerable < TEntity > entities )
2878+ {
2879+ if ( entities == null ) throw new ArgumentNullException ( "entities" ) ;
2880+ AddRange ( entities ) ;
2881+ }
2882+
2883+ public override EntityEntry < TEntity > Remove ( TEntity entity )
2884+ {
2885+ _data . Remove ( entity ) ;
2886+ return null ;
2887+ }
2888+
28572889 public override void RemoveRange ( params TEntity [ ] entities )
28582890 {
28592891 if ( entities == null ) throw new ArgumentNullException ( "entities" ) ;
@@ -2866,13 +2898,27 @@ public override void RemoveRange(IEnumerable<TEntity> entities)
28662898 RemoveRange ( entities . ToArray ( ) ) ;
28672899 }
28682900
2901+ public override EntityEntry < TEntity > Update ( TEntity entity )
2902+ {
2903+ _data . Remove ( entity ) ;
2904+ _data . Add ( entity ) ;
2905+ return null ;
2906+ }
2907+
28692908 public override void UpdateRange ( params TEntity [ ] entities )
28702909 {
28712910 if ( entities == null ) throw new ArgumentNullException ( "entities" ) ;
28722911 RemoveRange ( entities ) ;
28732912 AddRange ( entities ) ;
28742913 }
28752914
2915+ public override void UpdateRange ( IEnumerable < TEntity > entities )
2916+ {
2917+ if ( entities == null ) throw new ArgumentNullException ( "entities" ) ;
2918+ var array = entities . ToArray ( ) ; RemoveRange ( array ) ;
2919+ AddRange ( array ) ;
2920+ }
2921+
28762922 public IList GetList ( )
28772923 {
28782924 return _data ;
@@ -3076,6 +3122,83 @@ public class FakeExpressionVisitor : ExpressionVisitor
30763122 {
30773123 }
30783124
3125+ public class FakeDatabaseFacade : DatabaseFacade
3126+ {
3127+ public FakeDatabaseFacade ( DbContext context ) : base ( context )
3128+ {
3129+ }
3130+
3131+ public override bool EnsureCreated ( )
3132+ {
3133+ return true ;
3134+ }
3135+
3136+ public override Task < bool > EnsureCreatedAsync ( CancellationToken cancellationToken = new CancellationToken ( ) )
3137+ {
3138+ return Task . FromResult ( EnsureCreated ( ) ) ;
3139+ }
3140+
3141+ public override bool EnsureDeleted ( )
3142+ {
3143+ return true ;
3144+ }
3145+
3146+ public override Task < bool > EnsureDeletedAsync ( CancellationToken cancellationToken = new CancellationToken ( ) )
3147+ {
3148+ return Task . FromResult ( EnsureDeleted ( ) ) ;
3149+ }
3150+
3151+ public override bool CanConnect ( )
3152+ {
3153+ return true ;
3154+ }
3155+
3156+ public override Task < bool > CanConnectAsync ( CancellationToken cancellationToken = new CancellationToken ( ) )
3157+ {
3158+ return Task . FromResult ( CanConnect ( ) ) ;
3159+ }
3160+
3161+ public override IDbContextTransaction BeginTransaction ( )
3162+ {
3163+ return new FakeDbContextTransaction ( ) ;
3164+ }
3165+
3166+ public override Task < IDbContextTransaction > BeginTransactionAsync ( CancellationToken cancellationToken = new CancellationToken ( ) )
3167+ {
3168+ return Task . FromResult ( BeginTransaction ( ) ) ;
3169+ }
3170+
3171+ public override void CommitTransaction ( )
3172+ {
3173+ }
3174+
3175+ public override void RollbackTransaction ( )
3176+ {
3177+ }
3178+
3179+ public override IExecutionStrategy CreateExecutionStrategy ( )
3180+ {
3181+ return null ;
3182+ }
3183+
3184+ public override string ToString ( )
3185+ {
3186+ return string . Empty ;
3187+ }
3188+
3189+ }
3190+
3191+ public class FakeDbContextTransaction : IDbContextTransaction
3192+ {
3193+ public virtual Guid TransactionId => Guid . NewGuid ( ) ;
3194+ public virtual void Commit ( ) { }
3195+ public virtual void Rollback ( ) { }
3196+ public virtual Task CommitAsync ( CancellationToken cancellationToken = default ) => Task . CompletedTask ;
3197+ public virtual Task RollbackAsync ( CancellationToken cancellationToken = default ) => Task . CompletedTask ;
3198+ public virtual void Dispose ( ) { }
3199+ public virtual ValueTask DisposeAsync ( ) => default ;
3200+ }
3201+
30793202 #endregion
30803203
30813204 #region POCO classes
0 commit comments