update demo

This commit is contained in:
2881099
2022-09-18 22:22:26 +08:00
parent b0b7cb204c
commit 903a309c92
10 changed files with 122 additions and 92 deletions

View File

@ -0,0 +1,35 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace aspnetcore_transaction.Domain
{
public class SongRepository : DefaultRepository<Song, int>
{
public SongRepository(UnitOfWorkManager uowm) : base(uowm?.Orm, uowm) { }
}
[Description("123")]
public class Song
{
/// <summary>
/// 自增
/// </summary>
[Column(IsIdentity = true)]
[Description("自增id")]
public int Id { get; set; }
public string Title { get; set; }
}
public class Detail
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public int SongId { get; set; }
public string Title { get; set; }
}
}

View File

@ -16,7 +16,6 @@ namespace aspnetcore_transaction
{
webBuilder.UseStartup<Startup>();
})
//.UseServiceProviderFactory(new FreeSql.DynamicProxyServiceProviderFactory())
;
}
}

View File

@ -1,27 +0,0 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64513/",
"sslPort": 44328
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"dbcontext_01": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:35351/"
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using aspnetcore_transaction.Domain;
using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace aspnetcore_transaction.Services
{
public class SongService
{
BaseRepository<Song> _repoSong;
BaseRepository<Detail> _repoDetail;
SongRepository _repoSong2;
public SongService(BaseRepository<Song> repoSong, BaseRepository<Detail> repoDetail, SongRepository repoSong2)
{
var tb = repoSong.Orm.CodeFirst.GetTableByEntity(typeof(Song));
_repoSong = repoSong;
_repoDetail = repoDetail;
_repoSong2 = repoSong2;
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
public void Test1()
{
_repoSong.Insert(new Song());
_repoDetail.Insert(new Detail());
_repoSong2.Insert(new Song());
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
public Task Test11()
{
return Task.Delay(TimeSpan.FromSeconds(1)).ContinueWith(t =>
_repoSong.InsertAsync(new Song()));
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
public async Task Test2()
{
await _repoSong.InsertAsync(new Song());
await _repoDetail.InsertAsync(new Detail());
await _repoSong2.InsertAsync(new Song());
}
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
public async Task<object> Test3()
{
await _repoSong.InsertAsync(new Song());
await _repoDetail.InsertAsync(new Detail());
await _repoSong2.InsertAsync(new Song());
return "123";
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using aspnetcore_transaction.Controllers;
using FreeSql;
@ -35,7 +36,11 @@ namespace aspnetcore_transaction
services.AddSingleton<IFreeSql>(Fsql);
services.AddScoped<UnitOfWorkManager>();
services.AddFreeRepository(null, typeof(Startup).Assembly);
//批量注入
foreach (var repo in typeof(Startup).Assembly.GetTypes()
.Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a)))
services.AddScoped(repo);
services.AddScoped<SongService>();
}

View File

@ -1,54 +0,0 @@
//using FreeSql;
//using Microsoft.AspNetCore.Mvc.Filters;
//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.Text;
//using System.Threading.Tasks;
//namespace FreeSql
//{
// /// <summary>
// /// 使用事务执行,请查看 Program.cs 代码开启动态代理
// /// </summary>
// [AttributeUsage(AttributeTargets.Method)]
// public class TransactionalAttribute : DynamicProxyAttribute, IActionFilter
// {
// public Propagation Propagation { get; set; } = Propagation.Required;
// public IsolationLevel IsolationLevel { get => _IsolationLevelPriv.Value; set => _IsolationLevelPriv = value; }
// IsolationLevel? _IsolationLevelPriv;
// [DynamicProxyFromServices]
//#pragma warning disable IDE0044 // 添加只读修饰符
// UnitOfWorkManager _uowManager;
//#pragma warning restore IDE0044 // 添加只读修饰符
// IUnitOfWork _uow;
// public override Task Before(DynamicProxyBeforeArguments args) => OnBefore(_uowManager);
// public override Task After(DynamicProxyAfterArguments args) => OnAfter(args.Exception);
// //这里是为了 controller
// public void OnActionExecuting(ActionExecutingContext context) => OnBefore(context.HttpContext.RequestServices.GetService(typeof(UnitOfWorkManager)) as UnitOfWorkManager);
// public void OnActionExecuted(ActionExecutedContext context) => OnAfter(context.Exception);
// Task OnBefore(UnitOfWorkManager uowm)
// {
// _uow = uowm.Begin(this.Propagation, this._IsolationLevelPriv);
// return Task.FromResult(false);
// }
// Task OnAfter(Exception ex)
// {
// try
// {
// if (ex == null) _uow.Commit();
// else _uow.Rollback();
// }
// finally
// {
// _uow.Dispose();
// }
// return Task.FromResult(false);
// }
// }
//}

View File

@ -11,15 +11,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FreeSql.DbContext" Version="3.2.669" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.669" />
<PackageReference Include="Rougamo.Fody" Version="1.1.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
<PackageReference Include="FreeSql.DynamicProxy" Version="1.5.0" />
<PackageReference Include="IdleBus" Version="1.5.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
</ItemGroup>
</Project>

View File

@ -9,5 +9,10 @@
自增
</summary>
</member>
<member name="P:aspnetcore_transaction.Domain.Song.Id">
<summary>
自增
</summary>
</member>
</members>
</doc>