@@ -55,7 +55,7 @@ async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellati
5555 _db . OrmOriginal . SetEntityValueWithPropertyName ( _entityType , data , _tableIdentitys [ 0 ] . CsName , idtval ) ;
5656 _db . _entityChangeReport . Add ( new DbContext . EntityChangeReport . ChangeInfo { Object = data , Type = DbContext . EntityChangeType . Insert } ) ;
5757 Attach ( data ) ;
58- if ( _db . Options . EnableAddOrUpdateNavigate )
58+ if ( _db . Options . EnableCascadeSave )
5959 await AddOrUpdateNavigateAsync ( data , true , null , cancellationToken ) ;
6060 }
6161 else
@@ -66,7 +66,7 @@ async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellati
6666 IncrAffrows ( 1 ) ;
6767 _db . OrmOriginal . MapEntityValue ( _entityType , newval , data ) ;
6868 Attach ( newval ) ;
69- if ( _db . Options . EnableAddOrUpdateNavigate )
69+ if ( _db . Options . EnableCascadeSave )
7070 await AddOrUpdateNavigateAsync ( data , true , null , cancellationToken ) ;
7171 }
7272 return ;
@@ -79,7 +79,7 @@ async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellati
7979 _db . OrmOriginal . SetEntityValueWithPropertyName ( _entityType , data , _tableIdentitys [ 0 ] . CsName , idtval ) ;
8080 _db . _entityChangeReport . Add ( new DbContext . EntityChangeReport . ChangeInfo { Object = data , Type = DbContext . EntityChangeType . Insert } ) ;
8181 Attach ( data ) ;
82- if ( _db . Options . EnableAddOrUpdateNavigate )
82+ if ( _db . Options . EnableCascadeSave )
8383 await AddOrUpdateNavigateAsync ( data , true , null , cancellationToken ) ;
8484 return ;
8585 }
@@ -88,7 +88,7 @@ async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellati
8888 }
8989 EnqueueToDbContext ( DbContext . EntityChangeType . Insert , CreateEntityState ( data ) ) ;
9090 Attach ( data ) ;
91- if ( _db . Options . EnableAddOrUpdateNavigate )
91+ if ( _db . Options . EnableCascadeSave )
9292 await AddOrUpdateNavigateAsync ( data , true , null , cancellationToken ) ;
9393 }
9494 public Task AddAsync ( TEntity data , CancellationToken cancellationToken = default ) => AddPrivAsync ( data , true , cancellationToken ) ;
@@ -121,7 +121,7 @@ async public Task AddRangeAsync(IEnumerable<TEntity> data, CancellationToken can
121121 _db . OrmOriginal . MapEntityValue ( _entityType , rets [ idx ++ ] , s ) ;
122122 IncrAffrows ( rets . Count ) ;
123123 AttachRange ( rets ) ;
124- if ( _db . Options . EnableAddOrUpdateNavigate )
124+ if ( _db . Options . EnableCascadeSave )
125125 foreach ( var item in data )
126126 await AddOrUpdateNavigateAsync ( item , true , null , cancellationToken ) ;
127127 return ;
@@ -139,7 +139,7 @@ async public Task AddRangeAsync(IEnumerable<TEntity> data, CancellationToken can
139139 foreach ( var item in data )
140140 EnqueueToDbContext ( DbContext . EntityChangeType . Insert , CreateEntityState ( item ) ) ;
141141 AttachRange ( data ) ;
142- if ( _db . Options . EnableAddOrUpdateNavigate )
142+ if ( _db . Options . EnableCascadeSave )
143143 foreach ( var item in data )
144144 await AddOrUpdateNavigateAsync ( item , true , null , cancellationToken ) ;
145145 }
@@ -161,8 +161,8 @@ async public Task SaveManyAsync(TEntity item, string propertyName, CancellationT
161161 }
162162
163163 await DbContextFlushCommandAsync ( cancellationToken ) ;
164- var oldEnable = _db . Options . EnableAddOrUpdateNavigate ;
165- _db . Options . EnableAddOrUpdateNavigate = false ;
164+ var oldEnable = _db . Options . EnableCascadeSave ;
165+ _db . Options . EnableCascadeSave = false ;
166166 try
167167 {
168168 await AddOrUpdateNavigateAsync ( item , false , propertyName , cancellationToken ) ;
@@ -198,7 +198,7 @@ async public Task SaveManyAsync(TEntity item, string propertyName, CancellationT
198198 }
199199 finally
200200 {
201- _db . Options . EnableAddOrUpdateNavigate = oldEnable ;
201+ _db . Options . EnableCascadeSave = oldEnable ;
202202 }
203203 }
204204 async Task AddOrUpdateNavigateAsync ( TEntity item , bool isAdd , string propertyName , CancellationToken cancellationToken )
@@ -447,7 +447,7 @@ async Task UpdateRangePrivAsync(IEnumerable<TEntity> data, bool isCheck, Cancell
447447 state . OldValue = item ;
448448 EnqueueToDbContext ( DbContext . EntityChangeType . Update , state ) ;
449449 }
450- if ( _db . Options . EnableAddOrUpdateNavigate )
450+ if ( _db . Options . EnableCascadeSave )
451451 foreach ( var item in data )
452452 await AddOrUpdateNavigateAsync ( item , false , null , cancellationToken ) ;
453453 }
@@ -499,155 +499,11 @@ async public Task AddOrUpdateAsync(TEntity data, CancellationToken cancellationT
499499 #endregion
500500
501501 #region RemoveCascadeAsync
502- public Task < List < object > > RemoveCascadeAsync ( TEntity data , CancellationToken cancellationToken = default ) => RemoveRangeCascadeAsync ( new [ ] { data } , cancellationToken ) ;
503- public Task < List < object > > RemoveCascadeAsync ( Expression < Func < TEntity , bool > > predicate , CancellationToken cancellationToken = default ) => RemoveRangeCascadeAsync ( Select . Where ( predicate ) . ToList ( ) , cancellationToken ) ;
504- async public Task < List < object > > RemoveRangeCascadeAsync ( IEnumerable < TEntity > data , CancellationToken cancellationToken = default )
502+ public Task < List < object > > RemoveCascadeByDatabaseAsync ( Expression < Func < TEntity , bool > > predicate , CancellationToken cancellationToken = default ) => RemoveRangeCascadeByMemoryOrDatabaseAsync ( Select . Where ( predicate ) . ToList ( ) , false , cancellationToken ) ;
503+ async internal protected Task < List < object > > RemoveRangeCascadeByMemoryOrDatabaseAsync ( IEnumerable < TEntity > data , bool inMemory , CancellationToken cancellationToken = default )
505504 {
506- var returnDeleted = new List < object > ( ) ;
507- if ( data ? . Any ( ) != true ) return returnDeleted ;
508- await DbContextFlushCommandAsync ( cancellationToken ) ;
509- var fsql = _db . Orm ;
510- if ( LocalGetNavigates ( _table ) . Any ( ) == false )
511- {
512- if ( CanRemove ( data , true ) == false ) return returnDeleted ;
513- foreach ( var item in data ) //防止清除 Identity/Guid
514- {
515- var state = CreateEntityState ( item ) ;
516- _states . TryRemove ( state . Key , out var trystate ) ;
517-
518- EnqueueToDbContext ( DbContext . EntityChangeType . Delete , state ) ;
519- }
520- await DbContextFlushCommandAsync ( cancellationToken ) ;
521- returnDeleted . AddRange ( data . Select ( a => ( object ) a ) ) ;
522- return returnDeleted ;
523- }
524-
525- var commonUtils = ( fsql . Select < object > ( ) as Internal . CommonProvider . Select0Provider ) . _commonUtils ;
526- var eachdic = new Dictionary < string , bool > ( ) ;
527- var rootItems = data . Select ( a => ( object ) a ) . ToArray ( ) ;
528- var rootDbSet = _db . Set < object > ( ) ;
529- rootDbSet . AsType ( _table . Type ) ;
530- rootDbSet . AttachRange ( rootItems ) ;
531- await LocalEachAsync ( rootDbSet , rootItems , true ) ;
532- return returnDeleted ;
533-
534- List < NativeTuple < TableRef , PropertyInfo > > LocalGetNavigates ( TableInfo tb )
535- {
536- return tb . Properties . Where ( a => tb . ColumnsByCs . ContainsKey ( a . Key ) == false )
537- . Select ( a => new NativeTuple < TableRef , PropertyInfo > ( tb . GetTableRef ( a . Key , false ) , a . Value ) )
538- . Where ( a => a . Item1 != null && a . Item1 . RefType != TableRefType . ManyToOne )
539- . ToList ( ) ;
540- }
541- async Task LocalEachAsync ( DbSet < object > dbset , IEnumerable < object > items , bool isOneToOne )
542- {
543- items = items ? . Where ( item =>
544- {
545- var itemkeyStr = FreeSql . Extensions . EntityUtil . EntityUtilExtensions . GetEntityKeyString ( fsql , dbset . EntityType , item , false ) ;
546- var eachdicKey = $ "{ dbset . EntityType . FullName } ,{ itemkeyStr } ";
547- if ( eachdic . ContainsKey ( eachdicKey ) ) return false ;
548- eachdic . Add ( eachdicKey , true ) ;
549- return true ;
550- } ) . ToList ( ) ;
551- if ( items ? . Any ( ) != true ) return ;
552-
553- var tb = fsql . CodeFirst . GetTableByEntity ( dbset . EntityType ) ;
554- var navs = LocalGetNavigates ( tb ) ;
555-
556- var otos = navs . Where ( a => a . Item1 . RefType == TableRefType . OneToOne ) . ToList ( ) ;
557- if ( isOneToOne && otos . Any ( ) )
558- {
559- foreach ( var oto in otos )
560- {
561- var childTable = fsql . CodeFirst . GetTableByEntity ( oto . Item1 . RefEntityType ) ;
562- var childDbSet = _db . Set < object > ( ) ;
563- childDbSet . AsType ( oto . Item1 . RefEntityType ) ;
564- var refitems = items . Select ( item =>
565- {
566- var refitem = oto . Item1 . RefEntityType . CreateInstanceGetDefaultValue ( ) ;
567- for ( var a = 0 ; a < oto . Item1 . Columns . Count ; a ++ )
568- {
569- var colval = FreeSql . Extensions . EntityUtil . EntityUtilExtensions . GetPropertyValue ( tb , item , oto . Item1 . Columns [ a ] . CsName ) ;
570- FreeSql . Extensions . EntityUtil . EntityUtilExtensions . SetPropertyValue ( childTable , refitem , oto . Item1 . RefColumns [ a ] . CsName , colval ) ;
571- }
572- return refitem ;
573- } ) . ToList ( ) ;
574- var childs = await childDbSet . Select . Where ( commonUtils . WhereItems ( oto . Item1 . RefColumns . ToArray ( ) , "a." , refitems ) ) . ToListAsync ( false , cancellationToken ) ;
575- await LocalEachAsync ( childDbSet , childs , false ) ;
576- }
577- }
578-
579- var otms = navs . Where ( a => a . Item1 . RefType == TableRefType . OneToMany ) . ToList ( ) ;
580- if ( otms . Any ( ) )
581- {
582- foreach ( var otm in otms )
583- {
584- var childTable = fsql . CodeFirst . GetTableByEntity ( otm . Item1 . RefEntityType ) ;
585- var childDbSet = _db . Set < object > ( ) ;
586- childDbSet . AsType ( otm . Item1 . RefEntityType ) ;
587- var refitems = items . Select ( item =>
588- {
589- var refitem = otm . Item1 . RefEntityType . CreateInstanceGetDefaultValue ( ) ;
590- for ( var a = 0 ; a < otm . Item1 . Columns . Count ; a ++ )
591- {
592- var colval = FreeSql . Extensions . EntityUtil . EntityUtilExtensions . GetPropertyValue ( tb , item , otm . Item1 . Columns [ a ] . CsName ) ;
593- FreeSql . Extensions . EntityUtil . EntityUtilExtensions . SetPropertyValue ( childTable , refitem , otm . Item1 . RefColumns [ a ] . CsName , colval ) ;
594- }
595- return refitem ;
596- } ) . ToList ( ) ;
597- var childs = await childDbSet . Select . Where ( commonUtils . WhereItems ( otm . Item1 . RefColumns . ToArray ( ) , "a." , refitems ) ) . ToListAsync ( false , cancellationToken ) ;
598- await LocalEachAsync ( childDbSet , childs , true ) ;
599- }
600- }
601-
602- var mtms = navs . Where ( a => a . Item1 . RefType == TableRefType . ManyToMany ) . ToList ( ) ;
603- if ( mtms . Any ( ) )
604- {
605- foreach ( var mtm in mtms )
606- {
607- var childTable = fsql . CodeFirst . GetTableByEntity ( mtm . Item1 . RefMiddleEntityType ) ;
608- var childDbSet = _db . Set < object > ( ) ;
609- childDbSet . AsType ( mtm . Item1 . RefMiddleEntityType ) ;
610- var miditems = items . Select ( item =>
611- {
612- var refitem = mtm . Item1 . RefMiddleEntityType . CreateInstanceGetDefaultValue ( ) ;
613- for ( var a = 0 ; a < mtm . Item1 . Columns . Count ; a ++ )
614- {
615- var colval = FreeSql . Extensions . EntityUtil . EntityUtilExtensions . GetPropertyValue ( tb , item , mtm . Item1 . Columns [ a ] . CsName ) ;
616- FreeSql . Extensions . EntityUtil . EntityUtilExtensions . SetPropertyValue ( childTable , refitem , mtm . Item1 . MiddleColumns [ a ] . CsName , colval ) ;
617- }
618- return refitem ;
619- } ) . ToList ( ) ;
620- var childs = await childDbSet . Select . Where ( commonUtils . WhereItems ( mtm . Item1 . MiddleColumns . Take ( mtm . Item1 . Columns . Count ) . ToArray ( ) , "a." , miditems ) ) . ToListAsync ( false , cancellationToken ) ;
621- await LocalEachAsync ( childDbSet , childs , true ) ;
622- }
623- }
624-
625- if ( dbset == rootDbSet )
626- {
627- if ( CanRemove ( data , true ) == false ) return ;
628- foreach ( var item in data ) //防止清除 Identity/Guid
629- {
630- var state = CreateEntityState ( item ) ;
631- _states . TryRemove ( state . Key , out var trystate ) ;
632-
633- EnqueueToDbContext ( DbContext . EntityChangeType . Delete , state ) ;
634- }
635- await DbContextFlushCommandAsync ( cancellationToken ) ;
636- }
637- else
638- {
639- if ( dbset . CanRemove ( items , true ) == false ) return ;
640- foreach ( var item in items ) //防止清除 Identity/Guid
641- {
642- var state = dbset . CreateEntityState ( item ) ;
643- dbset . _states . TryRemove ( state . Key , out var trystate ) ;
644-
645- dbset . EnqueueToDbContext ( DbContext . EntityChangeType . Delete , state ) ;
646- }
647- await DbContextFlushCommandAsync ( cancellationToken ) ;
648- }
649- returnDeleted . AddRange ( items ) ;
650- }
505+ //临时调用同步方法,后续会改
506+ return await Task . FromResult ( RemoveRangeCascadeByMemoryOrDatabase ( data , inMemory ) ) ;
651507 }
652508 #endregion
653509 }
0 commit comments