mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
## v0.3.19
- 兼容 GetTableByEntity 有可能因为传入数组类型的错误; - 修复 UnitOfWork 事务创建逻辑 bug; - 增加 FreeSql.DbContext 扩展包; - 调整 UnitOfWork、DbContext 不提交时默认会回滚;
This commit is contained in:
parent
1dccf99bdb
commit
3fd971b78b
70
Examples/dbcontext_01/Controllers/ValuesController.cs
Normal file
70
Examples/dbcontext_01/Controllers/ValuesController.cs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace dbcontext_01.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class ValuesController : ControllerBase
|
||||||
|
{
|
||||||
|
|
||||||
|
IFreeSql _orm;
|
||||||
|
public ValuesController(SongContext songContext, IFreeSql orm) {
|
||||||
|
|
||||||
|
_orm = orm;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET api/values
|
||||||
|
[HttpGet]
|
||||||
|
async public Task<string> Get()
|
||||||
|
{
|
||||||
|
|
||||||
|
long id = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
using (var ctx = new SongContext()) {
|
||||||
|
|
||||||
|
id = await ctx.Songs.Insert(new Song { }).ExecuteIdentityAsync();
|
||||||
|
|
||||||
|
var item = await ctx.Songs.Select.Where(a => a.Id == id).FirstAsync();
|
||||||
|
|
||||||
|
throw new Exception("回滚");
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
var item = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
|
||||||
|
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET api/values/5
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public ActionResult<string> Get(int id)
|
||||||
|
{
|
||||||
|
return "value";
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST api/values
|
||||||
|
[HttpPost]
|
||||||
|
public void Post([FromBody] string value)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT api/values/5
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
public void Put(int id, [FromBody] string value)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE api/values/5
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public void Delete(int id)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
49
Examples/dbcontext_01/DbContexts/SongContext.cs
Normal file
49
Examples/dbcontext_01/DbContexts/SongContext.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using FreeSql;
|
||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace dbcontext_01 {
|
||||||
|
|
||||||
|
public class SongContext : DbContext {
|
||||||
|
|
||||||
|
public DbSet<Song> Songs { get; set; }
|
||||||
|
public DbSet<Song> Tags { get; set; }
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||||
|
builder.UseFreeSql(dbcontext_01.Startup.Fsql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class Song {
|
||||||
|
[Column(IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public DateTime? Create_time { get; set; }
|
||||||
|
public bool? Is_deleted { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
|
||||||
|
public virtual ICollection<Tag> Tags { get; set; }
|
||||||
|
}
|
||||||
|
public class Song_tag {
|
||||||
|
public int Song_id { get; set; }
|
||||||
|
public virtual Song Song { get; set; }
|
||||||
|
|
||||||
|
public int Tag_id { get; set; }
|
||||||
|
public virtual Tag Tag { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Tag {
|
||||||
|
[Column(IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int? Parent_id { get; set; }
|
||||||
|
public virtual Tag Parent { get; set; }
|
||||||
|
|
||||||
|
public decimal? Ddd { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public virtual ICollection<Song> Songs { get; set; }
|
||||||
|
public virtual ICollection<Tag> Tags { get; set; }
|
||||||
|
}
|
||||||
|
}
|
24
Examples/dbcontext_01/Program.cs
Normal file
24
Examples/dbcontext_01/Program.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace dbcontext_01
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
CreateWebHostBuilder(args).Build().Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||||
|
WebHost.CreateDefaultBuilder(args)
|
||||||
|
.UseStartup<Startup>();
|
||||||
|
}
|
||||||
|
}
|
68
Examples/dbcontext_01/Startup.cs
Normal file
68
Examples/dbcontext_01/Startup.cs
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
using FreeSql;
|
||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Swashbuckle.AspNetCore.Swagger;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace dbcontext_01
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
|
||||||
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
|
||||||
|
Fsql = new FreeSql.FreeSqlBuilder()
|
||||||
|
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10")
|
||||||
|
.UseLogger(loggerFactory.CreateLogger<IFreeSql>())
|
||||||
|
.UseAutoSyncStructure(true)
|
||||||
|
.UseLazyLoading(true)
|
||||||
|
|
||||||
|
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
|
||||||
|
.Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
public static IFreeSql Fsql { get; private set; }
|
||||||
|
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddMvc();
|
||||||
|
services.AddSwaggerGen(options => {
|
||||||
|
options.SwaggerDoc("v1", new Info {
|
||||||
|
Version = "v1",
|
||||||
|
Title = "FreeSql.DbContext API"
|
||||||
|
});
|
||||||
|
//options.IncludeXmlComments(xmlPath);
|
||||||
|
});
|
||||||
|
|
||||||
|
services.AddSingleton<IFreeSql>(Fsql);
|
||||||
|
services.AddFreeDbContext<SongContext>(options => options.UseFreeSql(Fsql));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
|
||||||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||||
|
Console.OutputEncoding = Encoding.GetEncoding("GB2312");
|
||||||
|
Console.InputEncoding = Encoding.GetEncoding("GB2312");
|
||||||
|
|
||||||
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
||||||
|
loggerFactory.AddDebug();
|
||||||
|
|
||||||
|
app.UseHttpMethodOverride(new HttpMethodOverrideOptions { FormFieldName = "X-Http-Method-Override" });
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
app.UseMvc();
|
||||||
|
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI(c => {
|
||||||
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FreeSql.RESTful API V1");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
Examples/dbcontext_01/appsettings.Development.json
Normal file
9
Examples/dbcontext_01/appsettings.Development.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Debug",
|
||||||
|
"System": "Information",
|
||||||
|
"Microsoft": "Information"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
Examples/dbcontext_01/appsettings.json
Normal file
8
Examples/dbcontext_01/appsettings.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
19
Examples/dbcontext_01/dbcontext_01.csproj
Normal file
19
Examples/dbcontext_01/dbcontext_01.csproj
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
|
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
|
||||||
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -51,7 +51,7 @@ namespace repository_01 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
public IFreeSql Fsql { get; }
|
public static IFreeSql Fsql { get; private set; }
|
||||||
|
|
||||||
public IServiceProvider ConfigureServices(IServiceCollection services) {
|
public IServiceProvider ConfigureServices(IServiceCollection services) {
|
||||||
|
|
||||||
|
94
FreeSql.DbContext/DbContext.cs
Normal file
94
FreeSql.DbContext/DbContext.cs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
using SafeObjectPool;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public abstract class DbContext : IDisposable {
|
||||||
|
|
||||||
|
internal IFreeSql _orm;
|
||||||
|
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||||
|
|
||||||
|
Object<DbConnection> _conn;
|
||||||
|
DbTransaction _tran;
|
||||||
|
|
||||||
|
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
|
||||||
|
protected DbContext() {
|
||||||
|
|
||||||
|
var builder = new DbContextOptionsBuilder();
|
||||||
|
OnConfiguring(builder);
|
||||||
|
_orm = builder._fsql;
|
||||||
|
|
||||||
|
var props = _dicGetDbSetProps.GetOrAdd(this.GetType(), tp =>
|
||||||
|
tp.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
||||||
|
.Where(a => a.PropertyType.IsGenericType &&
|
||||||
|
a.PropertyType == typeof(DbSet<>).MakeGenericType(a.PropertyType.GenericTypeArguments[0])).ToArray());
|
||||||
|
|
||||||
|
foreach (var prop in props) {
|
||||||
|
var set = this.Set(prop.PropertyType.GenericTypeArguments[0]);
|
||||||
|
|
||||||
|
prop.SetValue(this, set);
|
||||||
|
AllSets.Add(prop, set);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
|
||||||
|
public object Set(Type entityType) => Activator.CreateInstance(typeof(BaseDbSet<>).MakeGenericType(entityType), this);
|
||||||
|
|
||||||
|
protected Dictionary<PropertyInfo, object> AllSets => new Dictionary<PropertyInfo, object>();
|
||||||
|
|
||||||
|
public void SaveChanges() {
|
||||||
|
Commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReturnObject() {
|
||||||
|
_fsql.Ado.MasterPool.Return(_conn);
|
||||||
|
_tran = null;
|
||||||
|
_conn = null;
|
||||||
|
}
|
||||||
|
internal DbTransaction GetOrBeginTransaction(bool isCreate = true) {
|
||||||
|
|
||||||
|
if (_tran != null) return _tran;
|
||||||
|
if (isCreate == false) return null;
|
||||||
|
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
|
||||||
|
|
||||||
|
_conn = _fsql.Ado.MasterPool.Get();
|
||||||
|
try {
|
||||||
|
_tran = _conn.Value.BeginTransaction();
|
||||||
|
} catch {
|
||||||
|
ReturnObject();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return _tran;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Commit() {
|
||||||
|
if (_tran != null) {
|
||||||
|
try {
|
||||||
|
_tran.Commit();
|
||||||
|
} finally {
|
||||||
|
ReturnObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Rollback() {
|
||||||
|
if (_tran != null) {
|
||||||
|
try {
|
||||||
|
_tran.Rollback();
|
||||||
|
} finally {
|
||||||
|
ReturnObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void Dispose() {
|
||||||
|
this.Rollback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
FreeSql.DbContext/DbContextOptionsBuilder.cs
Normal file
17
FreeSql.DbContext/DbContextOptionsBuilder.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public class DbContextOptionsBuilder {
|
||||||
|
|
||||||
|
internal IFreeSql _fsql;
|
||||||
|
public DbContextOptionsBuilder UseFreeSql(IFreeSql orm) {
|
||||||
|
_fsql = orm;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
FreeSql.DbContext/DbSet.cs
Normal file
31
FreeSql.DbContext/DbSet.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public abstract class DbSet<TEntity> where TEntity : class {
|
||||||
|
|
||||||
|
protected DbContext _ctx;
|
||||||
|
|
||||||
|
public ISelect<TEntity> Select => _ctx._fsql.Select<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction(false));
|
||||||
|
|
||||||
|
public IInsert<TEntity> Insert(TEntity source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
|
public IInsert<TEntity> Insert(TEntity[] source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
|
public IInsert<TEntity> Insert(IEnumerable<TEntity> source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
|
|
||||||
|
public IUpdate<TEntity> Update => _ctx._fsql.Update<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
|
|
||||||
|
public IDelete<TEntity> Delete => _ctx._fsql.Delete<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
||||||
|
|
||||||
|
public BaseDbSet(DbContext ctx) {
|
||||||
|
_ctx = ctx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
FreeSql.DbContext/DependencyInjection.cs
Normal file
26
FreeSql.DbContext/DependencyInjection.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FreeSql {
|
||||||
|
public static class DbContextDependencyInjection {
|
||||||
|
|
||||||
|
public static IServiceCollection AddFreeDbContext<TDbContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> options) where TDbContext : DbContext {
|
||||||
|
|
||||||
|
services.AddScoped<TDbContext>(sp => {
|
||||||
|
var ctx = Activator.CreateInstance<TDbContext>();
|
||||||
|
|
||||||
|
if (ctx._orm == null) {
|
||||||
|
var builder = new DbContextOptionsBuilder();
|
||||||
|
options(builder);
|
||||||
|
ctx._orm = builder._fsql;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx;
|
||||||
|
});
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
FreeSql.DbContext/FreeSql.DbContext.csproj
Normal file
17
FreeSql.DbContext/FreeSql.DbContext.csproj
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<Version>0.3.19</Version>
|
||||||
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
<Authors>YeXiangQin</Authors>
|
||||||
|
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
||||||
|
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||||
|
<PackageTags>FreeSql ORM</PackageTags>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.3.18</Version>
|
<Version>0.3.19</Version>
|
||||||
<Authors>YeXiangQin</Authors>
|
<Authors>YeXiangQin</Authors>
|
||||||
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite, and read/write separation、and split table.</Description>
|
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite, and read/write separation、and split table.</Description>
|
||||||
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>
|
||||||
|
@ -94,7 +94,7 @@ namespace FreeSql {
|
|||||||
public Task<int> UpdateAsync(TEntity entity) => OrmUpdate(entity).ExecuteAffrowsAsync();
|
public Task<int> UpdateAsync(TEntity entity) => OrmUpdate(entity).ExecuteAffrowsAsync();
|
||||||
|
|
||||||
protected ISelect<TEntity> OrmSelect(object dywhere) {
|
protected ISelect<TEntity> OrmSelect(object dywhere) {
|
||||||
var select = _fsql.Select<TEntity>(dywhere).WithTransaction(_unitOfWork?.GetOrBeginTransaction());
|
var select = _fsql.Select<TEntity>(dywhere).WithTransaction(_unitOfWork?.GetOrBeginTransaction(false));
|
||||||
var filters = (DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
var filters = (DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
||||||
foreach (var filter in filters) select.Where(filter.Value.Expression);
|
foreach (var filter in filters) select.Where(filter.Value.Expression);
|
||||||
return select.AsTable(AsTableSelect);
|
return select.AsTable(AsTableSelect);
|
||||||
|
@ -12,8 +12,6 @@ namespace FreeSql {
|
|||||||
Object<DbConnection> _conn;
|
Object<DbConnection> _conn;
|
||||||
DbTransaction _tran;
|
DbTransaction _tran;
|
||||||
|
|
||||||
bool _isCommitOrRoolback = false;
|
|
||||||
|
|
||||||
public UnitOfWork(IFreeSql fsql) {
|
public UnitOfWork(IFreeSql fsql) {
|
||||||
_fsql = fsql;
|
_fsql = fsql;
|
||||||
}
|
}
|
||||||
@ -23,10 +21,10 @@ namespace FreeSql {
|
|||||||
_tran = null;
|
_tran = null;
|
||||||
_conn = null;
|
_conn = null;
|
||||||
}
|
}
|
||||||
internal DbTransaction GetOrBeginTransaction() {
|
internal DbTransaction GetOrBeginTransaction(bool isCreate = true) {
|
||||||
_isCommitOrRoolback = false;
|
|
||||||
|
|
||||||
if (_tran != null) return _tran;
|
if (_tran != null) return _tran;
|
||||||
|
if (isCreate == false) return null;
|
||||||
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
|
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
|
||||||
|
|
||||||
_conn = _fsql.Ado.MasterPool.Get();
|
_conn = _fsql.Ado.MasterPool.Get();
|
||||||
@ -43,27 +41,22 @@ namespace FreeSql {
|
|||||||
if (_tran != null) {
|
if (_tran != null) {
|
||||||
try {
|
try {
|
||||||
_tran.Commit();
|
_tran.Commit();
|
||||||
_isCommitOrRoolback = true;
|
|
||||||
} finally {
|
} finally {
|
||||||
ReturnObject();
|
ReturnObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void Rollback() {
|
public void Rollback() {
|
||||||
_isCommitOrRoolback = true;
|
|
||||||
if (_tran != null) {
|
if (_tran != null) {
|
||||||
try {
|
try {
|
||||||
_tran.Rollback();
|
_tran.Rollback();
|
||||||
_isCommitOrRoolback = true;
|
|
||||||
} finally {
|
} finally {
|
||||||
ReturnObject();
|
ReturnObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
if (_isCommitOrRoolback == false) {
|
this.Rollback();
|
||||||
this.Commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
public DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||||
<ProjectReference Include="..\FreeSql.Repository\FreeSql.Repository.csproj" />
|
<ProjectReference Include="..\FreeSql.Repository\FreeSql.Repository.csproj" />
|
||||||
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -28,10 +28,27 @@ namespace FreeSql.Tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ISelect<TestInfo> select => g.mysql.Select<TestInfo>();
|
ISelect<TestInfo> select => g.mysql.Select<TestInfo>();
|
||||||
|
|
||||||
|
|
||||||
|
class OrderContext : DbContext {
|
||||||
|
|
||||||
|
public DbSet<Order> Orders { get; set; }
|
||||||
|
public DbSet<OrderDetail> OrderDetails { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Test1() {
|
public void Test1() {
|
||||||
|
|
||||||
var parentSelect1 = select.Where(a => a.Type.Parent.Parent.Parent.Parent.Name == "").Where(b => b.Type.Name == "").ToSql();
|
using (var ctx = new OrderContext()) {
|
||||||
|
ctx.Orders.Insert(new Order { }).ExecuteAffrows();
|
||||||
|
ctx.Orders.Delete.Where(a => a.Id > 0).ExecuteAffrows();
|
||||||
|
|
||||||
|
ctx.OrderDetails.Select.Where(dt => dt.Order.Id == 10).ToList();
|
||||||
|
|
||||||
|
ctx.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
var parentSelect1 = select.Where(a => a.Type.Parent.Parent.Parent.Parent.Name == "").Where(b => b.Type.Name == "").ToSql();
|
||||||
|
|
||||||
|
|
||||||
var collSelect1 = g.mysql.Select<Order>().Where(a =>
|
var collSelect1 = g.mysql.Select<Order>().Where(a =>
|
||||||
|
29
FreeSql.sln
29
FreeSql.sln
@ -32,6 +32,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "net461_console_01", "Exampl
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "orm_vs", "Examples\orm_vs\orm_vs.csproj", "{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "orm_vs", "Examples\orm_vs\orm_vs.csproj", "{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeSql.DbContext", "FreeSql.DbContext\FreeSql.DbContext.csproj", "{E2D20A95-3045-49BD-973C-0CC6CEB957DB}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dbcontext_01", "Examples\dbcontext_01\dbcontext_01.csproj", "{9ED752FF-F908-4611-827A-E99655607567}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -174,6 +178,30 @@ Global
|
|||||||
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x64.Build.0 = Release|Any CPU
|
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x86.ActiveCfg = Release|Any CPU
|
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x86.Build.0 = Release|Any CPU
|
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@ -185,6 +213,7 @@ Global
|
|||||||
{A23D0455-CA7B-442D-827E-C4C7E84F9084} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
{A23D0455-CA7B-442D-827E-C4C7E84F9084} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
||||||
{0637A778-338E-4096-B439-32B18306C75F} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
{0637A778-338E-4096-B439-32B18306C75F} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
||||||
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
||||||
|
{9ED752FF-F908-4611-827A-E99655607567} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {089687FD-5D25-40AB-BA8A-A10D1E137F98}
|
SolutionGuid = {089687FD-5D25-40AB-BA8A-A10D1E137F98}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.3.18</Version>
|
<Version>0.3.19</Version>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>YeXiangQin</Authors>
|
<Authors>YeXiangQin</Authors>
|
||||||
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
||||||
|
@ -27,6 +27,7 @@ namespace FreeSql.Internal {
|
|||||||
if (tbc.TryGetValue(entity, out var trytb)) return trytb;
|
if (tbc.TryGetValue(entity, out var trytb)) return trytb;
|
||||||
if (common.CodeFirst.GetDbInfo(entity) != null) return null;
|
if (common.CodeFirst.GetDbInfo(entity) != null) return null;
|
||||||
if (typeof(IEnumerable).IsAssignableFrom(entity)) return null;
|
if (typeof(IEnumerable).IsAssignableFrom(entity)) return null;
|
||||||
|
if (entity.IsArray) return null;
|
||||||
|
|
||||||
var tbattr = common.GetEntityTableAttribute(entity);
|
var tbattr = common.GetEntityTableAttribute(entity);
|
||||||
trytb = new TableInfo();
|
trytb = new TableInfo();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user