@@ -18,7 +18,7 @@ public class UnitOfWorkManager : IDisposable
1818 public IFreeSql Orm => _ormScoped ;
1919 List < UowInfo > _rawUows = new List < UowInfo > ( ) ;
2020 List < UowInfo > _allUows = new List < UowInfo > ( ) ;
21- List < RepoInfo > _repos = new List < RepoInfo > ( ) ;
21+ List < BindInfo > _binds = new List < BindInfo > ( ) ;
2222
2323 public UnitOfWorkManager ( IFreeSql fsql )
2424 {
@@ -53,7 +53,7 @@ public void Dispose()
5353 {
5454 _rawUows . Clear ( ) ;
5555 _allUows . Clear ( ) ;
56- _repos . Clear ( ) ;
56+ _binds . Clear ( ) ;
5757 GC . SuppressFinalize ( this ) ;
5858 }
5959 }
@@ -70,15 +70,30 @@ public void Dispose()
7070 /// <param name="repository"></param>
7171 public void Binding ( IBaseRepository repository )
7272 {
73- var repoInfo = new RepoInfo ( repository ) ;
73+ var bind = new BindInfo ( repository ) ;
7474 repository . UnitOfWork = Current ;
75- if ( _repos . Any ( a => a . Repository == repository ) ) return ;
76- _repos . Add ( repoInfo ) ;
75+ if ( _binds . Any ( a => a . Repository == repository ) ) return ;
76+ _binds . Add ( bind ) ;
7777 }
78- void SetAllRepositoryUow ( )
78+ /// <summary>
79+ /// 将DbContext的事务交给我管理
80+ /// </summary>
81+ /// <param name="dbContext"></param>
82+ public void Binding ( DbContext dbContext )
83+ {
84+ var bind = new BindInfo ( dbContext ) ;
85+ dbContext . _isUseUnitOfWork = false ;
86+ dbContext . UnitOfWork = Current ;
87+ if ( _binds . Any ( a => a . DbContext == dbContext ) ) return ;
88+ _binds . Add ( bind ) ;
89+ }
90+ void SetAllBindsUow ( )
7991 {
80- foreach ( var repo in _repos )
81- repo . Repository . UnitOfWork = Current ?? repo . OrginalUow ;
92+ foreach ( var bind in _binds )
93+ {
94+ if ( bind . Repository != null ) bind . Repository . UnitOfWork = Current ?? bind . OrginalUow ;
95+ if ( bind . DbContext != null ) bind . DbContext . UnitOfWork = Current ?? bind . OrginalUow ;
96+ }
8297 }
8398
8499 /// <summary>
@@ -121,7 +136,7 @@ IUnitOfWork FindedUowCreateVirtual()
121136 var uowInfo = new UowInfo ( uow , UowInfo . UowType . Virtual , isNotSupported ) ;
122137 uow . OnDispose = ( ) => _allUows . Remove ( uowInfo ) ;
123138 _allUows . Add ( uowInfo ) ;
124- SetAllRepositoryUow ( ) ;
139+ SetAllBindsUow ( ) ;
125140 return uow ;
126141 }
127142 }
@@ -133,7 +148,7 @@ IUnitOfWork CreateUowNothing(bool isNotSupported)
133148 var uowInfo = new UowInfo ( uow , UowInfo . UowType . Nothing , isNotSupported ) ;
134149 uow . OnDispose = ( ) => _allUows . Remove ( uowInfo ) ;
135150 _allUows . Add ( uowInfo ) ;
136- SetAllRepositoryUow ( ) ;
151+ SetAllBindsUow ( ) ;
137152 return uow ;
138153 }
139154 IUnitOfWork CreateUow ( IsolationLevel ? isolationLevel )
@@ -148,24 +163,30 @@ IUnitOfWork CreateUow(IsolationLevel? isolationLevel)
148163 {
149164 _rawUows . Remove ( uowInfo ) ;
150165 _allUows . Remove ( uowInfo ) ;
151- SetAllRepositoryUow ( ) ;
166+ SetAllBindsUow ( ) ;
152167 } ;
153168 _rawUows . Add ( uowInfo ) ;
154169 _allUows . Add ( uowInfo ) ;
155- SetAllRepositoryUow ( ) ;
170+ SetAllBindsUow ( ) ;
156171 return uow ;
157172 }
158173
159- class RepoInfo
174+ class BindInfo
160175 {
176+ public DbContext DbContext ;
161177 public IBaseRepository Repository ;
162178 public IUnitOfWork OrginalUow ;
163179
164- public RepoInfo ( IBaseRepository repository )
180+ public BindInfo ( IBaseRepository repository )
165181 {
166182 this . Repository = repository ;
167183 this . OrginalUow = repository . UnitOfWork ;
168184 }
185+ public BindInfo ( DbContext dbContext )
186+ {
187+ this . DbContext = dbContext ;
188+ this . OrginalUow = dbContext . UnitOfWork ;
189+ }
169190 }
170191 class UowInfo
171192 {
0 commit comments