mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	update UnitOfWorkManager demo
This commit is contained in:
		@@ -2,6 +2,7 @@
 | 
				
			|||||||
using FreeSql.DataAnnotations;
 | 
					using FreeSql.DataAnnotations;
 | 
				
			||||||
using Microsoft.AspNetCore.Mvc;
 | 
					using Microsoft.AspNetCore.Mvc;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace aspnetcore_transaction.Controllers
 | 
					namespace aspnetcore_transaction.Controllers
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -16,12 +17,22 @@ namespace aspnetcore_transaction.Controllers
 | 
				
			|||||||
            _logger = logger;
 | 
					            _logger = logger;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [HttpGet]
 | 
					        [HttpGet("1")]
 | 
				
			||||||
        //[Transactional]
 | 
					        //[Transactional]
 | 
				
			||||||
        virtual public object Get([FromServices] BaseRepository<Song> repoSong, [FromServices] BaseRepository<Detail> repoDetail, [FromServices] SongRepository repoSong2,
 | 
					        virtual public object Get([FromServices] BaseRepository<Song> repoSong, [FromServices] BaseRepository<Detail> repoDetail, [FromServices] SongRepository repoSong2,
 | 
				
			||||||
            [FromServices] SongService serviceSong)
 | 
					            [FromServices] SongService serviceSong)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            serviceSong.Test();
 | 
					            serviceSong.Test1();
 | 
				
			||||||
 | 
					            return "111";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [HttpGet("2")]
 | 
				
			||||||
 | 
					        //[Transactional]
 | 
				
			||||||
 | 
					        async virtual public Task<object> GetAsync([FromServices] BaseRepository<Song> repoSong, [FromServices] BaseRepository<Detail> repoDetail, [FromServices] SongRepository repoSong2,
 | 
				
			||||||
 | 
					           [FromServices] SongService serviceSong)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            await serviceSong.Test2();
 | 
				
			||||||
 | 
					            await serviceSong.Test3();
 | 
				
			||||||
            return "111";
 | 
					            return "111";
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -40,12 +51,29 @@ namespace aspnetcore_transaction.Controllers
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
 | 
					        [Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
 | 
				
			||||||
        public virtual void Test()
 | 
					        public virtual void Test1()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _repoSong.Insert(new Song());
 | 
					            _repoSong.Insert(new Song());
 | 
				
			||||||
            _repoDetail.Insert(new Detail());
 | 
					            _repoDetail.Insert(new Detail());
 | 
				
			||||||
            _repoSong2.Insert(new Song());
 | 
					            _repoSong2.Insert(new Song());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
 | 
				
			||||||
 | 
					        async public virtual Task Test2()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            await _repoSong.InsertAsync(new Song());
 | 
				
			||||||
 | 
					            await _repoDetail.InsertAsync(new Detail());
 | 
				
			||||||
 | 
					            await _repoSong2.InsertAsync(new Song());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
 | 
				
			||||||
 | 
					        async public virtual Task<object> Test3()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            await _repoSong.InsertAsync(new Song());
 | 
				
			||||||
 | 
					            await _repoDetail.InsertAsync(new Detail());
 | 
				
			||||||
 | 
					            await _repoSong2.InsertAsync(new Song());
 | 
				
			||||||
 | 
					            return "123";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public class SongRepository : DefaultRepository<Song, int>
 | 
					    public class SongRepository : DefaultRepository<Song, int>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,6 +28,9 @@ namespace aspnetcore_transaction
 | 
				
			|||||||
                 .UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
 | 
					                 .UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
 | 
				
			||||||
                 .UseNoneCommandParameter(true)
 | 
					                 .UseNoneCommandParameter(true)
 | 
				
			||||||
                 .Build();
 | 
					                 .Build();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            Fsql.Aop.TraceBefore += (_, e) => Trace.WriteLine($"----TraceBefore---{e.Identifier} {e.Operation}");
 | 
				
			||||||
 | 
					            Fsql.Aop.TraceAfter += (_, e) => Trace.WriteLine($"----TraceAfter---{e.Identifier} {e.Operation} {e.Remark} {e.Exception?.Message} {e.ElapsedMilliseconds}ms\r\n");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public IConfiguration Configuration { get; }
 | 
					        public IConfiguration Configuration { get; }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,7 +5,7 @@
 | 
				
			|||||||
  </PropertyGroup>
 | 
					  </PropertyGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
    <PackageReference Include="FreeSql.DynamicProxy" Version="1.2.0" />
 | 
					    <PackageReference Include="FreeSql.DynamicProxy" Version="1.3.0" />
 | 
				
			||||||
    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
 | 
					    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
 | 
				
			||||||
  </ItemGroup>
 | 
					  </ItemGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user