- 合并 FreeSql.DbContext 项目至 FreeSql 维护;

This commit is contained in:
28810 2019-06-26 10:09:26 +08:00
parent a708062c97
commit 611c066481
185 changed files with 4577 additions and 95 deletions

View File

@ -0,0 +1,241 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FreeSql;
using Microsoft.AspNetCore.Mvc;
namespace dbcontext_01.Controllers {
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase {
IFreeSql _orm;
SongContext _songContext;
public ValuesController(SongContext songContext,
IFreeSql orm1, IFreeSql orm2,
IFreeSql<long> orm3
) {
_songContext = songContext;
_orm = orm1;
}
// GET api/values
[HttpGet]
async public Task<string> Get() {
long id = 0;
try {
var repos2Song = _orm.GetRepository<Song, int>();
repos2Song.Where(a => a.Id > 10).ToList();
//查询结果,进入 states
var song = new Song { };
repos2Song.Insert(song);
id = song.Id;
var adds = Enumerable.Range(0, 100)
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
.ToList();
//创建一堆无主键值
repos2Song.Insert(adds);
for (var a = 0; a < 10; a++)
adds[a].Title = "dkdkdkdk" + a;
repos2Song.Update(adds);
//批量修改
repos2Song.Delete(adds.Skip(10).Take(20).ToList());
//批量删除10-20 元素的主键值会被清除
adds.Last().Url = "skldfjlksdjglkjjcccc";
repos2Song.Update(adds.Last());
adds.First().Url = "skldfjlksdjglkjjcccc";
repos2Song.Update(adds.First());
var ctx = _songContext;
var tag = new Tag {
Name = "testaddsublist",
Tags = new[] {
new Tag { Name = "sub1" },
new Tag { Name = "sub2" },
new Tag {
Name = "sub3",
Tags = new[] {
new Tag { Name = "sub3_01" }
}
}
}
};
ctx.Tags.Add(tag);
ctx.UnitOfWork.GetOrBeginTransaction();
var tagAsync = new Tag {
Name = "testaddsublist",
Tags = new[] {
new Tag { Name = "sub1" },
new Tag { Name = "sub2" },
new Tag {
Name = "sub3",
Tags = new[] {
new Tag { Name = "sub3_01" }
}
}
}
};
await ctx.Tags.AddAsync(tagAsync);
ctx.Songs.Select.Where(a => a.Id > 10).ToList();
//查询结果,进入 states
song = new Song { };
//可插入的 song
ctx.Songs.Add(song);
id = song.Id;
//因有自增类型立即开启事务执行SQL返回自增值
adds = Enumerable.Range(0, 100)
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
.ToList();
//创建一堆无主键值
ctx.Songs.AddRange(adds);
//立即执行,将自增值赋给 adds 所有元素,因为有自增类型,如果其他类型,指定传入主键值,不会立即执行
for (var a = 0; a < 10; a++)
adds[a].Title = "dkdkdkdk" + a;
ctx.Songs.UpdateRange(adds);
//批量修改,进入队列
ctx.Songs.RemoveRange(adds.Skip(10).Take(20).ToList());
//批量删除,进入队列,完成时 10-20 元素的主键值会被清除
//ctx.Songs.Update(adds.First());
adds.Last().Url = "skldfjlksdjglkjjcccc";
ctx.Songs.Update(adds.Last());
adds.First().Url = "skldfjlksdjglkjjcccc";
ctx.Songs.Update(adds.First());
//单条修改 urls 的值,进入队列
//throw new Exception("回滚");
//ctx.Songs.Select.First();
//这里做一个查询,会立即打包【执行队列】,避免没有提交的数据,影响查询结果
ctx.SaveChanges();
//打包【执行队列】,提交事务
using (var uow = _orm.CreateUnitOfWork()) {
var reposSong = uow.GetRepository<Song, int>();
reposSong.Where(a => a.Id > 10).ToList();
//查询结果,进入 states
song = new Song { };
reposSong.Insert(song);
id = song.Id;
adds = Enumerable.Range(0, 100)
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
.ToList();
//创建一堆无主键值
reposSong.Insert(adds);
for (var a = 0; a < 10; a++)
adds[a].Title = "dkdkdkdk" + a;
reposSong.Update(adds);
//批量修改
reposSong.Delete(adds.Skip(10).Take(20).ToList());
//批量删除10-20 元素的主键值会被清除
adds.Last().Url = "skldfjlksdjglkjjcccc";
reposSong.Update(adds.Last());
adds.First().Url = "skldfjlksdjglkjjcccc";
reposSong.Update(adds.First());
uow.Commit();
}
//using (var ctx = new SongContext()) {
// var song = new Song { };
// await ctx.Songs.AddAsync(song);
// id = song.Id;
// var adds = Enumerable.Range(0, 100)
// .Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
// .ToList();
// await ctx.Songs.AddRangeAsync(adds);
// for (var a = 0; a < adds.Count; a++)
// adds[a].Title = "dkdkdkdk" + a;
// ctx.Songs.UpdateRange(adds);
// ctx.Songs.RemoveRange(adds.Skip(10).Take(20).ToList());
// //ctx.Songs.Update(adds.First());
// adds.Last().Url = "skldfjlksdjglkjjcccc";
// ctx.Songs.Update(adds.Last());
// //throw new Exception("回滚");
// await ctx.SaveChangesAsync();
//}
} catch {
var item = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
throw;
}
var item22 = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
var item33 = await _orm.Select<Song>().Where(a => a.Id > id).ToListAsync();
return item22.Id.ToString();
}
// 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) {
}
}
}

View File

@ -0,0 +1,52 @@
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<Tag> 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; }
[Column(IsVersion = true)]
public long versionRow { 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; }
}
}

View File

@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace dbcontext_01
{
public class Program
{
public class Song {
[Column(IsIdentity = true)]
public int Id { get; set; }
public string BigNumber { get; set; }
[Column(IsVersion = true)]//使用简单
public long versionRow { get; set; }
}
public class SongContext : DbContext {
public DbSet<Song> Songs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
builder.UseFreeSql(fsql);
}
}
static IFreeSql fsql;
public static void Main(string[] args) {
fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\dd2.db;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseNoneCommandParameter(true)
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
.Build();
using (var ctx = new SongContext()) {
var song = new Song { BigNumber = "1000000000000000000" };
ctx.Songs.Add(song);
ctx.Songs.Update(song);
song.BigNumber = (BigInteger.Parse(song.BigNumber) + 1).ToString();
ctx.Songs.Update(song);
ctx.SaveChanges();
var sql = fsql.Update<Song>().SetSource(song).ToSql();
}
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}

View File

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

View File

@ -0,0 +1,111 @@
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|\document2.db;Pooling=true;Max Pool Size=10")
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
//.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=10")
//.UseSyncStructureToUpper(true)
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseNoneCommandParameter(true)
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText),
(cmd, log) => Trace.WriteLine(log)
)
.Build();
Fsql.Aop.SyncStructureBefore = (s, e) => {
Console.WriteLine(e.Identifier + ": " + string.Join(", ", e.EntityTypes.Select(a => a.FullName)));
};
Fsql.Aop.SyncStructureAfter = (s, e) => {
Console.WriteLine(e.Identifier + ": " + string.Join(", ", e.EntityTypes.Select(a => a.FullName)) + " " + e.ElapsedMilliseconds + "ms\r\n" + e.Exception?.Message + e.Sql);
};
Fsql.Aop.CurdBefore = (s, e) => {
Console.WriteLine(e.Identifier + ": " + e.EntityType.FullName + ", " + e.Sql);
};
Fsql.Aop.CurdAfter = (s, e) => {
Console.WriteLine(e.Identifier + ": " + e.EntityType.FullName + " " + e.ElapsedMilliseconds + "ms, " + e.Sql);
};
Fsql2 = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document222.db;Pooling=true;Max Pool Size=10")
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText),
(cmd, log) => Trace.WriteLine(log)
)
.Build<long>();
}
enum MySql { }
enum PgSql { }
public IConfiguration Configuration { get; }
public static IFreeSql Fsql { get; private set; }
public static IFreeSql<long> Fsql2 { 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.AddSingleton<IFreeSql<long>>(Fsql2);
services.AddFreeDbContext<SongContext>(options => options.UseFreeSql(Fsql));
var sql1 = Fsql.Update<Song>(1).Set(a => a.Id + 10).ToSql();
var sql2 = Fsql.Update<Song>(1).Set(a => a.Title + 10).ToSql();
var sql3 = Fsql.Update<Song>(1).Set(a => a.Create_time.Value.AddHours(1)).ToSql();
}
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");
});
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FreeSql.Extensions.LazyLoading" Version="0.6.5" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="0.6.8" />
<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>

View File

@ -0,0 +1,98 @@
using FreeSql;
using Microsoft.AspNetCore.Mvc;
using restful.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace restful.Controllers {
public class SongRepository : GuidRepository<Song> {
public SongRepository(IFreeSql fsql) : base(fsql) {
}
}
[Route("restapi/[controller]")]
public class SongsController : Controller {
BaseRepository<Song, int> _songRepository;
public class xxxx {
public int Id { get; set; }
public bool IsDeleted { get; set; }
}
public SongsController(IFreeSql fsql,
GuidRepository<Song> repos1,
GuidRepository<xxxx> repos2,
DefaultRepository<Song, int> repos11,
DefaultRepository<xxxx, int> repos21,
BaseRepository<Song> repos3, BaseRepository<Song, int> repos4,
IBasicRepository<Song> repos31, IBasicRepository<Song, int> repos41,
IReadOnlyRepository<Song> repos311, IReadOnlyRepository<Song, int> repos411,
SongRepository reposSong
) {
_songRepository = repos4;
//test code
var curd1 = fsql.GetRepository<Song, int>();
var curd2 = fsql.GetRepository<Song, string>();
var curd3 = fsql.GetRepository<Song, Guid>();
var curd4 = fsql.GetGuidRepository<Song>();
Console.WriteLine(repos1.Select.ToSql());
Console.WriteLine(reposSong.Select.ToSql());
Console.WriteLine(repos2.Select.ToSql());
Console.WriteLine(repos21.Select.ToSql());
using (reposSong.DataFilter.DisableAll()) {
Console.WriteLine(reposSong.Select.ToSql());
}
}
[HttpGet]
public Task<List<Song>> GetItems([FromQuery] string key, [FromQuery] int page = 1, [FromQuery] int limit = 20) {
return _songRepository.Select.WhereIf(!string.IsNullOrEmpty(key), a => a.Title.Contains(key)).Page(page, limit).ToListAsync();
}
[HttpGet("{id}")]
public Task<Song> GetItem([FromRoute] int id) {
return _songRepository.FindAsync(id);
}
public class ModelSong {
public string title { get; set; }
}
[HttpPost, ProducesResponseType(201)]
public Task<Song> Create([FromBody] ModelSong model) {
return _songRepository.InsertAsync(new Song { Title = model.title });
}
[HttpPut("{id}")]
public Task Update([FromRoute] int id, [FromBody] ModelSong model) {
return _songRepository.UpdateAsync(new Song { Id = id, Title = model.title });
}
[HttpPatch("{id}")]
async public Task<Song> UpdateDiy([FromRoute] int id, [FromForm] string title) {
var up = _songRepository.UpdateDiy.Where(a => a.Id == id);
if (!string.IsNullOrEmpty(title)) up.Set(a => a.Title, title);
var ret = await up.ExecuteUpdatedAsync();
return ret.FirstOrDefault();
}
[HttpDelete("{id}"), ProducesResponseType(204)]
public Task Delete([FromRoute] int id) {
return _songRepository.DeleteAsync(a => a.Id == id);
}
}
}

View File

@ -0,0 +1,11 @@
using FreeSql.DataAnnotations;
using repository_01;
namespace restful.Entitys {
public class Song {
[Column(IsIdentity = true)]
public int Id { get; set; }
public string Title { get; set; }
}
}

View 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 repository_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>();
}
}

View File

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

View File

@ -0,0 +1,98 @@
using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using restful.Entitys;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace repository_01 {
/// <summary>
/// 用户密码信息
/// </summary>
public class Sys1UserLogOn {
[Column(IsPrimary = true, Name = "Id")]
public Guid UserLogOnId { get; set; }
public virtual Sys1User User { get; set; }
}
public class Sys1User {
[Column(IsPrimary = true, Name = "Id")]
public Guid UserId { get; set; }
public virtual Sys1UserLogOn UserLogOn { get; set; }
}
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")
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
.Build();
var sysu = new Sys1User { };
Fsql.Insert<Sys1User>().AppendData(sysu).ExecuteAffrows();
Fsql.Insert<Sys1UserLogOn>().AppendData(new Sys1UserLogOn { UserLogOnId = sysu.UserId }).ExecuteAffrows();
var a = Fsql.Select<Sys1UserLogOn>().ToList();
var b = Fsql.Select<Sys1UserLogOn>().Any();
}
public IConfiguration Configuration { get; }
public static IFreeSql Fsql { get; private set; }
public void ConfigureServices(IServiceCollection services) {
//services.AddTransient(s => s.)
services.AddMvc();
services.AddSwaggerGen(options => {
options.SwaggerDoc("v1", new Info {
Version = "v1",
Title = "FreeSql.RESTful API"
});
//options.IncludeXmlComments(xmlPath);
});
services.AddSingleton<IFreeSql>(Fsql);
services.AddFreeRepository(filter => {
filter
//.Apply<Song>("test", a => a.Title == DateTime.Now.ToString() + System.Threading.Thread.CurrentThread.ManagedThreadId)
.Apply<ISoftDelete>("softdelete", a => a.IsDeleted == false);
}, this.GetType().Assembly);
}
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");
});
}
}
public interface ISoftDelete {
bool IsDeleted { get; set; }
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Warning",
"Microsoft": "Warning"
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FreeSql.Provider.Sqlite" Version="0.6.8" />
<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>

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks> <TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.6.13</Version> <Version>0.7.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors> <Authors>YeXiangQin</Authors>
<Description>FreeSql 扩展包,可实现【延时加载】属性.</Description> <Description>FreeSql 扩展包,可实现【延时加载】属性.</Description>

View File

@ -0,0 +1,154 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace FreeSql {
public abstract partial class DbContext : IDisposable {
internal IFreeSql _orm;
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
public IFreeSql Orm => _fsql;
protected IUnitOfWork _uowPriv;
internal IUnitOfWork _uow => _isUseUnitOfWork ? (_uowPriv ?? (_uowPriv = new UnitOfWork(_fsql))) : null;
internal bool _isUseUnitOfWork = true; //不使用工作单元事务
public IUnitOfWork UnitOfWork => _uow;
DbContextOptions _options;
internal DbContextOptions Options {
get {
if (_options != null) return _options;
if (FreeSqlDbContextExtenssions._dicSetDbContextOptions.TryGetValue(Orm, out _options)) return _options;
_options = new DbContextOptions();
return _options;
}
}
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
protected DbContext() {
var builder = new DbContextOptionsBuilder();
OnConfiguring(builder);
_orm = builder._fsql;
if (_orm != null) InitPropSets();
}
internal void InitPropSets() {
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.Name, set);
}
}
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
}
protected Dictionary<Type, IDbSet> _dicSet = new Dictionary<Type, IDbSet>();
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
public virtual IDbSet Set(Type entityType) {
if (_dicSet.ContainsKey(entityType)) return _dicSet[entityType];
var sd = Activator.CreateInstance(typeof(DbContextDbSet<>).MakeGenericType(entityType), this) as IDbSet;
if (entityType != typeof(object)) _dicSet.Add(entityType, sd);
return sd;
}
protected Dictionary<string, IDbSet> AllSets { get; } = new Dictionary<string, IDbSet>();
#region DbSet
/// <summary>
/// 添加
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="data"></param>
public void Add<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Add(data);
public void AddRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AddRange(data);
public Task AddAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddAsync(data);
public Task AddRangeAsync<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AddRangeAsync(data);
/// <summary>
/// 更新
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="data"></param>
public void Update<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Update(data);
public void UpdateRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().UpdateRange(data);
public Task UpdateAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().UpdateAsync(data);
public Task UpdateRangeAsync<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().UpdateRangeAsync(data);
/// <summary>
/// 删除
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="data"></param>
public void Remove<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Remove(data);
public void RemoveRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().RemoveRange(data);
/// <summary>
/// 添加或更新
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="data"></param>
public void AddOrUpdate<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddOrUpdate(data);
public Task AddOrUpdateAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddOrUpdateAsync(data);
/// <summary>
/// 附加实体,可用于不查询就更新或删除
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="data"></param>
public void Attach<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Attach(data);
public void AttachRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AttachRange(data);
#endregion
internal class ExecCommandInfo {
public ExecCommandInfoType actionType { get; set; }
public IDbSet dbSet { get; set; }
public Type stateType { get; set; }
public object state { get; set; }
}
internal enum ExecCommandInfoType { Insert, Update, Delete }
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
internal int _affrows = 0;
internal void EnqueueAction(ExecCommandInfoType actionType, IDbSet dbSet, Type stateType, object state) {
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
}
~DbContext() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
try {
_actions.Clear();
foreach (var set in _dicSet)
try {
set.Value.Dispose();
} catch { }
_dicSet.Clear();
AllSets.Clear();
_uow?.Rollback();
} finally {
_isdisposed = true;
GC.SuppressFinalize(this);
}
}
}
}

View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace FreeSql {
partial class DbContext {
async public virtual Task<int> SaveChangesAsync() {
await ExecCommandAsync();
_uow?.Commit();
var ret = _affrows;
_affrows = 0;
return ret;
}
static Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>> _dicExecCommandDbContextBetchAsync = new Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>>();
async internal Task ExecCommandAsync() {
if (isExecCommanding) return;
if (_actions.Any() == false) return;
isExecCommanding = true;
ExecCommandInfo oldinfo = null;
var states = new List<object>();
Func<string, Task<int>> dbContextBetch = methodName => {
if (_dicExecCommandDbContextBetchAsync.TryGetValue(oldinfo.stateType, out var trydic) == false)
trydic = new Dictionary<string, Func<object, object[], Task<int>>>();
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
var arrType = oldinfo.stateType.MakeArrayType();
var dbsetType = oldinfo.dbSet.GetType().BaseType;
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var returnTarget = Expression.Label(typeof(Task<int>));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
tryfunc = Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(global::FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
), new[] { parm1DbSet, parm2Vals }).Compile();
trydic.Add(methodName, tryfunc);
}
return tryfunc(oldinfo.dbSet, states.ToArray());
};
Func<Task> funcDelete = async () => {
_affrows += await dbContextBetch("DbContextBetchRemoveAsync");
states.Clear();
};
Func<Task> funcInsert = async () => {
_affrows += await dbContextBetch("DbContextBetchAddAsync");
states.Clear();
};
Func<bool, Task> funcUpdate = async (isLiveUpdate) => {
var affrows = 0;
if (isLiveUpdate) affrows = await dbContextBetch("DbContextBetchUpdateNowAsync");
else affrows = await dbContextBetch("DbContextBetchUpdateAsync");
if (affrows == -999) { //最后一个元素已被删除
states.RemoveAt(states.Count - 1);
return;
}
if (affrows == -998 || affrows == -997) { //没有执行更新
var laststate = states[states.Count - 1];
states.Clear();
if (affrows == -997) states.Add(laststate); //保留最后一个
}
if (affrows > 0) {
_affrows += affrows;
var islastNotUpdated = states.Count != affrows;
var laststate = states[states.Count - 1];
states.Clear();
if (islastNotUpdated) states.Add(laststate); //保留最后一个
}
};
while (_actions.Any() || states.Any()) {
var info = _actions.Any() ? _actions.Dequeue() : null;
if (oldinfo == null) oldinfo = info;
var isLiveUpdate = false;
if (_actions.Any() == false && states.Any() ||
info != null && oldinfo.actionType != info.actionType ||
info != null && oldinfo.stateType != info.stateType) {
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
//最后一个,合起来发送
states.Add(info.state);
info = null;
}
switch (oldinfo.actionType) {
case ExecCommandInfoType.Insert:
await funcInsert();
break;
case ExecCommandInfoType.Delete:
await funcDelete();
break;
}
isLiveUpdate = true;
}
if (isLiveUpdate || oldinfo.actionType == ExecCommandInfoType.Update) {
if (states.Any())
await funcUpdate(isLiveUpdate);
}
if (info != null) {
states.Add(info.state);
oldinfo = info;
}
}
isExecCommanding = false;
}
}
}

View File

@ -0,0 +1,10 @@

namespace FreeSql {
public class DbContextOptions {
/// <summary>
/// 是否开启一对多,联级保存功能
/// </summary>
public bool EnableAddOrUpdateNavigateList { get; set; } = true;
}
}

View File

@ -0,0 +1,13 @@

namespace FreeSql {
public class DbContextOptionsBuilder {
internal IFreeSql _fsql;
public DbContextOptionsBuilder UseFreeSql(IFreeSql orm) {
_fsql = orm;
return this;
}
}
}

View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
namespace FreeSql {
partial class DbContext {
public virtual int SaveChanges() {
ExecCommand();
_uow?.Commit();
var ret = _affrows;
_affrows = 0;
return ret;
}
static Dictionary<Type, Dictionary<string, Func<object, object[], int>>> _dicExecCommandDbContextBetch = new Dictionary<Type, Dictionary<string, Func<object, object[], int>>>();
bool isExecCommanding = false;
internal void ExecCommand() {
if (isExecCommanding) return;
if (_actions.Any() == false) return;
isExecCommanding = true;
ExecCommandInfo oldinfo = null;
var states = new List<object>();
Func<string, int> dbContextBetch = methodName => {
if (_dicExecCommandDbContextBetch.TryGetValue(oldinfo.stateType, out var trydic) == false)
trydic = new Dictionary<string, Func<object, object[], int>>();
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
var arrType = oldinfo.stateType.MakeArrayType();
var dbsetType = oldinfo.dbSet.GetType().BaseType;
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var returnTarget = Expression.Label(typeof(int));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
tryfunc = Expression.Lambda<Func<object, object[], int>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(global::FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
Expression.Label(returnTarget, Expression.Default(typeof(int)))
), new[] { parm1DbSet, parm2Vals }).Compile();
trydic.Add(methodName, tryfunc);
}
return tryfunc(oldinfo.dbSet, states.ToArray());
};
Action funcDelete = () => {
_affrows += dbContextBetch("DbContextBetchRemove");
states.Clear();
};
Action funcInsert = () => {
_affrows += dbContextBetch("DbContextBetchAdd");
states.Clear();
};
Action<bool> funcUpdate = isLiveUpdate => {
var affrows = 0;
if (isLiveUpdate) affrows = dbContextBetch("DbContextBetchUpdateNow");
else affrows = dbContextBetch("DbContextBetchUpdate");
if (affrows == -999) { //最后一个元素已被删除
states.RemoveAt(states.Count - 1);
return;
}
if (affrows == -998 || affrows == -997) { //没有执行更新
var laststate = states[states.Count - 1];
states.Clear();
if (affrows == -997) states.Add(laststate); //保留最后一个
}
if (affrows > 0) {
_affrows += affrows;
var islastNotUpdated = states.Count != affrows;
var laststate = states[states.Count - 1];
states.Clear();
if (islastNotUpdated) states.Add(laststate); //保留最后一个
}
};
while (_actions.Any() || states.Any()) {
var info = _actions.Any() ? _actions.Dequeue() : null;
if (oldinfo == null) oldinfo = info;
var isLiveUpdate = false;
if (_actions.Any() == false && states.Any() ||
info != null && oldinfo.actionType != info.actionType ||
info != null && oldinfo.stateType != info.stateType) {
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
//最后一个,合起来发送
states.Add(info.state);
info = null;
}
switch (oldinfo.actionType) {
case ExecCommandInfoType.Insert:
funcInsert();
break;
case ExecCommandInfoType.Delete:
funcDelete();
break;
}
isLiveUpdate = true;
}
if (isLiveUpdate || oldinfo.actionType == ExecCommandInfoType.Update) {
if (states.Any())
funcUpdate(isLiveUpdate);
}
if (info != null) {
states.Add(info.state);
oldinfo = info;
}
}
isExecCommanding = false;
}
}
}

View File

@ -0,0 +1,10 @@

namespace FreeSql {
public class FreeContext : DbContext {
public FreeContext(IFreeSql orm) {
_orm = orm;
}
}
}

View File

@ -0,0 +1,281 @@
using FreeSql.Extensions.EntityUtil;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace FreeSql {
internal class DbContextDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
public DbContextDbSet(DbContext ctx) {
_ctx = ctx;
_uow = ctx._uow;
_fsql = ctx._fsql;
}
}
public interface IDbSet : IDisposable {
Type EntityType { get; }
}
public abstract partial class DbSet<TEntity> : IDbSet where TEntity : class {
internal DbContext _ctx;
internal IUnitOfWork _uow;
internal IFreeSql _fsql;
protected virtual ISelect<TEntity> OrmSelect(object dywhere) {
DbContextExecCommand(); //查询前先提交,否则会出脏读
return _fsql.Select<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction(false)).TrackToList(TrackToList).WhereDynamic(dywhere);
}
~DbSet() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
try {
this._dicUpdateTimes.Clear();
this._states.Clear();
} finally {
_isdisposed = true;
GC.SuppressFinalize(this);
}
}
protected virtual IInsert<TEntity> OrmInsert() => _fsql.Insert<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction());
protected virtual IInsert<TEntity> OrmInsert(TEntity data) => _fsql.Insert<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction()).AppendData(data);
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data) => _fsql.Insert<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction()).AppendData(data);
protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys) => _fsql.Update<TEntity>().AsType(_entityType).SetSource(entitys).WithTransaction(_uow?.GetOrBeginTransaction());
protected virtual IDelete<TEntity> OrmDelete(object dywhere) => _fsql.Delete<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction()).WhereDynamic(dywhere);
internal void EnqueueToDbContext(DbContext.ExecCommandInfoType actionType, EntityState state) {
_ctx.EnqueueAction(actionType, this, typeof(EntityState), state);
}
internal void IncrAffrows(int affrows) {
_ctx._affrows += affrows;
}
internal void TrackToList(object list) {
if (list == null) return;
var ls = list as IList<TEntity>;
if (ls == null) {
var ie = list as IEnumerable;
if (ie == null) return;
foreach (var item in ie) {
if (item == null) return;
var itemType = item.GetType();
if (itemType == typeof(object)) return;
if (itemType.FullName.StartsWith("Submission#")) itemType = itemType.BaseType;
var dbset = _ctx.Set(itemType);
dbset?.GetType().GetMethod("TrackToList", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(dbset, new object[] { list });
return;
}
return;
}
foreach (var item in ls) {
var key = _fsql.GetEntityKeyString(_entityType, item, false);
if (key == null) continue;
_states.AddOrUpdate(key, k => CreateEntityState(item), (k, ov) => {
_fsql.MapEntityValue(_entityType, item, ov.Value);
ov.Time = DateTime.Now;
return ov;
});
}
}
public ISelect<TEntity> Select => this.OrmSelect(null);
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).Where(exp);
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).WhereIf(condition, exp);
protected ConcurrentDictionary<string, EntityState> _states = new ConcurrentDictionary<string, EntityState>();
internal ConcurrentDictionary<string, EntityState> _statesInternal => _states;
TableInfo _tablePriv;
protected TableInfo _table => _tablePriv ?? (_tablePriv = _fsql.CodeFirst.GetTableByEntity(_entityType));
ColumnInfo[] _tableIdentitysPriv;
protected ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray());
protected Type _entityType = typeof(TEntity);
public Type EntityType => _entityType;
/// <summary>
/// 动态Type在使用 DbSet&lt;object&gt; 后使用本方法,指定实体类型
/// </summary>
/// <param name="entityType"></param>
/// <returns></returns>
public void AsType(Type entityType) {
if (entityType == typeof(object)) throw new Exception("ISelect.AsType 参数不支持指定为 object");
if (entityType == _entityType) return;
var newtb = _fsql.CodeFirst.GetTableByEntity(entityType);
_entityType = entityType;
_tablePriv = newtb ?? throw new Exception("DbSet.AsType 参数错误,请传入正确的实体类型");
_tableIdentitysPriv = null;
}
public class EntityState {
public EntityState(TEntity value, string key) {
this.Value = value;
this.Key = key;
this.Time = DateTime.Now;
}
public TEntity OldValue { get; set; }
public TEntity Value { get; set; }
public string Key { get; set; }
public DateTime Time { get; set; }
}
/// <summary>
/// 附加实体,可用于不查询就更新或删除
/// </summary>
/// <param name="data"></param>
public void Attach(TEntity data) => AttachRange(new[] { data });
public void AttachRange(IEnumerable<TEntity> data) {
if (data == null || data.Any() == false) return;
if (_table.Primarys.Any() == false) throw new Exception($"不可附加,实体没有主键:{_fsql.GetEntityString(_entityType, data.First())}");
foreach (var item in data) {
var key = _fsql.GetEntityKeyString(_entityType, item, false);
if (string.IsNullOrEmpty(key)) throw new Exception($"不可附加,未设置主键的值:{_fsql.GetEntityString(_entityType, item)}");
_states.AddOrUpdate(key, k => CreateEntityState(item), (k, ov) => {
_fsql.MapEntityValue(_entityType, item, ov.Value);
ov.Time = DateTime.Now;
return ov;
});
}
}
/// <summary>
/// 清空状态数据
/// </summary>
public void FlushState() {
_states.Clear();
}
#region Utils
EntityState CreateEntityState(TEntity data) {
if (data == null) throw new ArgumentNullException(nameof(data));
var key = _fsql.GetEntityKeyString(_entityType, data, false);
var state = new EntityState((TEntity)Activator.CreateInstance(_entityType), key);
_fsql.MapEntityValue(_entityType, data, state.Value);
return state;
}
bool? ExistsInStates(TEntity data) {
if (data == null) throw new ArgumentNullException(nameof(data));
var key = _fsql.GetEntityKeyString(_entityType, data, false);
if (string.IsNullOrEmpty(key)) return null;
return _states.ContainsKey(key);
}
bool CanAdd(IEnumerable<TEntity> data, bool isThrow) {
if (data == null) {
if (isThrow) throw new ArgumentNullException(nameof(data));
return false;
}
if (data.Any() == false) return false;
foreach (var s in data) if (CanAdd(s, isThrow) == false) return false;
return true;
}
bool CanAdd(TEntity data, bool isThrow) {
if (data == null) {
if (isThrow) throw new ArgumentNullException(nameof(data));
return false;
}
if (_table.Primarys.Any() == false) {
if (isThrow) throw new Exception($"不可添加,实体没有主键:{_fsql.GetEntityString(_entityType, data)}");
return false;
}
var key = _fsql.GetEntityKeyString(_entityType, data, true);
if (string.IsNullOrEmpty(key)) {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return true;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
return true;
}
if (isThrow) throw new Exception($"不可添加,未设置主键的值:{_fsql.GetEntityString(_entityType, data)}");
return false;
}
} else {
if (_states.ContainsKey(key)) {
if (isThrow) throw new Exception($"不可添加,已存在于状态管理:{_fsql.GetEntityString(_entityType, data)}");
return false;
}
var idval = _fsql.GetEntityIdentityValueWithPrimary(_entityType, data);
if (idval > 0) {
if (isThrow) throw new Exception($"不可添加,自增属性有值:{_fsql.GetEntityString(_entityType, data)}");
return false;
}
}
return true;
}
bool CanUpdate(IEnumerable<TEntity> data, bool isThrow) {
if (data == null) {
if (isThrow) throw new ArgumentNullException(nameof(data));
return false;
}
if (data.Any() == false) return false;
foreach (var s in data) if (CanUpdate(s, isThrow) == false) return false;
return true;
}
bool CanUpdate(TEntity data, bool isThrow) {
if (data == null) {
if (isThrow) throw new ArgumentNullException(nameof(data));
return false;
}
if (_table.Primarys.Any() == false) {
if (isThrow) throw new Exception($"不可更新,实体没有主键:{_fsql.GetEntityString(_entityType, data)}");
return false;
}
var key = _fsql.GetEntityKeyString(_entityType, data, false);
if (string.IsNullOrEmpty(key)) {
if (isThrow) throw new Exception($"不可更新,未设置主键的值:{_fsql.GetEntityString(_entityType, data)}");
return false;
}
if (_states.TryGetValue(key, out var tryval) == false) {
if (isThrow) throw new Exception($"不可更新,数据未被跟踪,应该先查询 或者 Attach{_fsql.GetEntityString(_entityType, data)}");
return false;
}
return true;
}
bool CanRemove(IEnumerable<TEntity> data, bool isThrow) {
if (data == null) {
if (isThrow) throw new ArgumentNullException(nameof(data));
return false;
}
if (data.Any() == false) return false;
foreach (var s in data) if (CanRemove(s, isThrow) == false) return false;
return true;
}
bool CanRemove(TEntity data, bool isThrow) {
if (data == null) {
if (isThrow) throw new ArgumentNullException(nameof(data));
return false;
}
if (_table.Primarys.Any() == false) {
if (isThrow) throw new Exception($"不可删除,实体没有主键:{_fsql.GetEntityString(_entityType, data)}");
return false;
}
var key = _fsql.GetEntityKeyString(_entityType, data, false);
if (string.IsNullOrEmpty(key)) {
if (isThrow) throw new Exception($"不可删除,未设置主键的值:{_fsql.GetEntityString(_entityType, data)}");
return false;
}
//if (_states.TryGetValue(key, out var tryval) == false) {
// if (isThrow) throw new Exception($"不可删除,数据未被跟踪,应该先查询:{_fsql.GetEntityString(_entityType, data)}");
// return false;
//}
return true;
}
#endregion
}
}

View File

@ -0,0 +1,268 @@
using FreeSql.Extensions.EntityUtil;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FreeSql {
partial class DbSet<TEntity> {
Task DbContextExecCommandAsync() {
_dicUpdateTimes.Clear();
return _ctx.ExecCommandAsync();
}
async Task<int> DbContextBetchAddAsync(EntityState[] adds) {
if (adds.Any() == false) return 0;
var affrows = await this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrowsAsync();
return affrows;
}
#region Add
async Task AddPrivAsync(TEntity data, bool isCheck) {
if (isCheck && CanAdd(data, true) == false) return;
if (_tableIdentitys.Length > 0) {
//有自增,马上执行
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
await DbContextExecCommandAsync();
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
IncrAffrows(1);
_fsql.SetEntityIdentityValueWithPrimary(_entityType, data, idtval);
Attach(data);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
await AddOrUpdateNavigateListAsync(data);
} else {
await DbContextExecCommandAsync();
var newval = (await this.OrmInsert(data).ExecuteInsertedAsync()).First();
IncrAffrows(1);
_fsql.MapEntityValue(_entityType, newval, data);
Attach(newval);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
await AddOrUpdateNavigateListAsync(data);
}
return;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
await DbContextExecCommandAsync();
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
IncrAffrows(1);
_fsql.SetEntityIdentityValueWithPrimary(_entityType, data, idtval);
Attach(data);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
await AddOrUpdateNavigateListAsync(data);
}
return;
}
}
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
Attach(data);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
await AddOrUpdateNavigateListAsync(data);
}
public Task AddAsync(TEntity data) => AddPrivAsync(data, true);
async public Task AddRangeAsync(IEnumerable<TEntity> data) {
if (CanAdd(data, true) == false) return;
if (data.ElementAtOrDefault(1) == default(TEntity)) {
await AddAsync(data.First());
return;
}
if (_tableIdentitys.Length > 0) {
//有自增,马上执行
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
await DbContextExecCommandAsync();
var rets = await this.OrmInsert(data).ExecuteInsertedAsync();
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
var idx = 0;
foreach (var s in data)
_fsql.MapEntityValue(_entityType, rets[idx++], s);
IncrAffrows(rets.Count);
AttachRange(rets);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
await AddOrUpdateNavigateListAsync(item);
return;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
foreach (var s in data)
await AddPrivAsync(s, false);
return;
}
} else {
//进入队列,等待 SaveChanges 时执行
foreach (var item in data)
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(item));
AttachRange(data);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
await AddOrUpdateNavigateListAsync(item);
}
}
async Task AddOrUpdateNavigateListAsync(TEntity item) {
Type itemType = null;
foreach (var prop in _table.Properties) {
if (_table.ColumnsByCsIgnore.ContainsKey(prop.Key)) continue;
if (_table.ColumnsByCs.ContainsKey(prop.Key)) continue;
var tref = _table.GetTableRef(prop.Key, true);
if (tref == null) continue;
switch (tref.RefType) {
case Internal.Model.TableRefType.OneToOne:
case Internal.Model.TableRefType.ManyToOne:
case Internal.Model.TableRefType.ManyToMany:
continue;
case Internal.Model.TableRefType.OneToMany:
if (itemType == null) itemType = item.GetType();
if (_table.TypeLazy != null && itemType == _table.TypeLazy) {
var lazyField = _dicLazyIsSetField.GetOrAdd(_table.TypeLazy, tl => new ConcurrentDictionary<string, System.Reflection.FieldInfo>()).GetOrAdd(prop.Key, propName =>
_table.TypeLazy.GetField($"__lazy__{propName}", System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance));
if (lazyField != null) {
var lazyFieldValue = (bool)lazyField.GetValue(item);
if (lazyFieldValue == false) continue;
}
}
var propVal = prop.Value.GetValue(item);
var propValEach = propVal as IEnumerable;
if (propValEach == null) continue;
object dbset = null;
System.Reflection.MethodInfo dbsetAddOrUpdate = null;
foreach (var propValItem in propValEach) {
if (dbset == null) {
dbset = _ctx.Set(tref.RefEntityType);
dbsetAddOrUpdate = dbset.GetType().GetMethod("AddOrUpdateAsync", new Type[] { tref.RefEntityType });
}
for (var colidx = 0; colidx < tref.Columns.Count; colidx++) {
tref.RefColumns[colidx].Table.Properties[tref.RefColumns[colidx].CsName]
.SetValue(propValItem, tref.Columns[colidx].Table.Properties[tref.Columns[colidx].CsName].GetValue(item));
}
Task task = dbsetAddOrUpdate.Invoke(dbset, new object[] { propValItem }) as Task;
await task;
}
break;
}
}
}
#endregion
#region UpdateAsync
Task<int> DbContextBetchUpdateAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, false);
Task<int> DbContextBetchUpdateNowAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, true);
async Task<int> DbContextBetchUpdatePrivAsync(EntityState[] ups, bool isLiveUpdate) {
if (ups.Any() == false) return 0;
var uplst1 = ups[ups.Length - 1];
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
var lstval2 = default(EntityState);
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_fsql.GetEntityString(_entityType, uplst2.Value)}");
var cuig1 = _fsql.CompareEntityValueReturnColumns(_entityType, uplst1.Value, lstval1.Value, true);
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(_entityType, uplst2.Value, lstval2.Value, true) : null;
List<EntityState> data = null;
string[] cuig = null;
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
//最后一个不保存
data = ups.ToList();
data.RemoveAt(ups.Length - 1);
cuig = cuig2;
} else if (isLiveUpdate) {
//立即保存
data = ups.ToList();
cuig = cuig1;
}
if (data?.Count > 0) {
if (cuig.Length == _table.Columns.Count)
return ups.Length == data.Count ? -998 : -997;
var updateSource = data.Select(a => a.Value).ToArray();
var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig);
var affrows = await update.ExecuteAffrowsAsync();
foreach (var newval in data) {
if (_states.TryGetValue(newval.Key, out var tryold))
_fsql.MapEntityValue(_entityType, newval.Value, tryold.Value);
if (newval.OldValue != null)
_fsql.MapEntityValue(_entityType, newval.Value, newval.OldValue);
}
return affrows;
}
//等待下次对比再保存
return 0;
}
async public Task UpdateAsync(TEntity data) {
var exists = ExistsInStates(data);
if (exists == null) throw new Exception($"不可更新,未设置主键的值:{_fsql.GetEntityString(_entityType, data)}");
if (exists == false) {
var olddata = await OrmSelect(data).FirstAsync();
if (olddata == null) throw new Exception($"不可更新,数据库不存在该记录:{_fsql.GetEntityString(_entityType, data)}");
}
await UpdateRangePrivAsync(new[] { data }, true);
}
public Task UpdateRangeAsync(IEnumerable<TEntity> data) => UpdateRangePrivAsync(data, true);
async Task UpdateRangePrivAsync(IEnumerable<TEntity> data, bool isCheck) {
if (CanUpdate(data, true) == false) return;
foreach (var item in data) {
if (_dicUpdateTimes.ContainsKey(item))
await DbContextExecCommandAsync();
_dicUpdateTimes.Add(item, 1);
var state = CreateEntityState(item);
state.OldValue = item;
EnqueueToDbContext(DbContext.ExecCommandInfoType.Update, state);
}
if (_ctx.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
await AddOrUpdateNavigateListAsync(item);
}
#endregion
#region RemoveAsync
async Task<int> DbContextBetchRemoveAsync(EntityState[] dels) {
if (dels.Any() == false) return 0;
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
return Math.Max(dels.Length, affrows);
}
#endregion
#region AddOrUpdateAsync
async public Task AddOrUpdateAsync(TEntity data) {
if (data == null) throw new ArgumentNullException(nameof(data));
if (_table.Primarys.Any() == false) throw new Exception($"不可添加,实体没有主键:{_fsql.GetEntityString(_entityType, data)}");
var flagExists = ExistsInStates(data);
if (flagExists == false) {
var olddata = await OrmSelect(data).FirstAsync();
if (olddata == null) flagExists = false;
}
if (flagExists == true && CanUpdate(data, false)) {
await DbContextExecCommandAsync();
var affrows = _ctx._affrows;
await UpdateRangePrivAsync(new[] { data }, false);
await DbContextExecCommandAsync();
affrows = _ctx._affrows - affrows;
if (affrows > 0) return;
}
if (CanAdd(data, false)) {
_fsql.ClearEntityPrimaryValueWithIdentity(_entityType, data);
await AddPrivAsync(data, false);
}
}
#endregion
}
}

View File

@ -0,0 +1,305 @@
using FreeSql.Extensions.EntityUtil;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
namespace FreeSql {
partial class DbSet<TEntity> {
void DbContextExecCommand() {
_dicUpdateTimes.Clear();
_ctx.ExecCommand();
}
int DbContextBetchAdd(EntityState[] adds) {
if (adds.Any() == false) return 0;
var affrows = this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrows();
return affrows;
}
#region Add
void AddPriv(TEntity data, bool isCheck) {
if (isCheck && CanAdd(data, true) == false) return;
if (_tableIdentitys.Length > 0) {
//有自增,马上执行
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
if (_tableIdentitys.Length == 1) {
DbContextExecCommand();
var idtval = this.OrmInsert(data).ExecuteIdentity();
IncrAffrows(1);
_fsql.SetEntityIdentityValueWithPrimary(_entityType, data, idtval);
Attach(data);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
AddOrUpdateNavigateList(data);
} else {
DbContextExecCommand();
var newval = this.OrmInsert(data).ExecuteInserted().First();
IncrAffrows(1);
_fsql.MapEntityValue(_entityType, newval, data);
Attach(newval);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
AddOrUpdateNavigateList(data);
}
return;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
if (_tableIdentitys.Length == 1) {
DbContextExecCommand();
var idtval = this.OrmInsert(data).ExecuteIdentity();
IncrAffrows(1);
_fsql.SetEntityIdentityValueWithPrimary(_entityType, data, idtval);
Attach(data);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
AddOrUpdateNavigateList(data);
}
return;
}
}
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
Attach(data);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
AddOrUpdateNavigateList(data);
}
/// <summary>
/// 添加
/// </summary>
/// <param name="data"></param>
public void Add(TEntity data) => AddPriv(data, true);
public void AddRange(IEnumerable<TEntity> data) {
if (CanAdd(data, true) == false) return;
if (data.ElementAtOrDefault(1) == default(TEntity)) {
Add(data.First());
return;
}
if (_tableIdentitys.Length > 0) {
//有自增,马上执行
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
DbContextExecCommand();
var rets = this.OrmInsert(data).ExecuteInserted();
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
var idx = 0;
foreach (var s in data)
_fsql.MapEntityValue(_entityType, rets[idx++], s);
IncrAffrows(rets.Count);
AttachRange(rets);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
AddOrUpdateNavigateList(item);
return;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
foreach (var s in data)
AddPriv(s, false);
return;
}
} else {
//进入队列,等待 SaveChanges 时执行
foreach (var item in data)
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(item));
AttachRange(data);
if (_ctx.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
AddOrUpdateNavigateList(item);
}
}
static ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>> _dicLazyIsSetField = new ConcurrentDictionary<Type, ConcurrentDictionary<string, FieldInfo>>();
void AddOrUpdateNavigateList(TEntity item) {
Type itemType = null;
foreach (var prop in _table.Properties) {
if (_table.ColumnsByCsIgnore.ContainsKey(prop.Key)) continue;
if (_table.ColumnsByCs.ContainsKey(prop.Key)) continue;
object propVal = null;
if (itemType == null) itemType = item.GetType();
if (_table.TypeLazy != null && itemType == _table.TypeLazy) {
var lazyField = _dicLazyIsSetField.GetOrAdd(_table.TypeLazy, tl => new ConcurrentDictionary<string, FieldInfo>()).GetOrAdd(prop.Key, propName =>
_table.TypeLazy.GetField($"__lazy__{propName}", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance));
if (lazyField != null) {
var lazyFieldValue = (bool)lazyField.GetValue(item);
if (lazyFieldValue == false) continue;
}
propVal = prop.Value.GetValue(item);
} else {
propVal = prop.Value.GetValue(item);
if (propVal == null) continue;
}
var tref = _table.GetTableRef(prop.Key, true);
if (tref == null) continue;
switch(tref.RefType) {
case Internal.Model.TableRefType.OneToOne:
case Internal.Model.TableRefType.ManyToOne:
case Internal.Model.TableRefType.ManyToMany:
continue;
case Internal.Model.TableRefType.OneToMany:
var propValEach = propVal as IEnumerable;
if (propValEach == null) continue;
object dbset = null;
MethodInfo dbsetAddOrUpdate = null;
foreach (var propValItem in propValEach) {
if (dbset == null) {
dbset = _ctx.Set(tref.RefEntityType);
dbsetAddOrUpdate = dbset.GetType().GetMethod("AddOrUpdate", new Type[] { tref.RefEntityType });
}
for (var colidx = 0; colidx < tref.Columns.Count; colidx++) {
tref.RefColumns[colidx].Table.Properties[tref.RefColumns[colidx].CsName]
.SetValue(propValItem, tref.Columns[colidx].Table.Properties[tref.Columns[colidx].CsName].GetValue(item));
}
dbsetAddOrUpdate.Invoke(dbset, new object[] { propValItem });
}
break;
}
}
}
#endregion
#region Update
int DbContextBetchUpdate(EntityState[] ups) => DbContextBetchUpdatePriv(ups, false);
int DbContextBetchUpdateNow(EntityState[] ups) => DbContextBetchUpdatePriv(ups, true);
int DbContextBetchUpdatePriv(EntityState[] ups, bool isLiveUpdate) {
if (ups.Any() == false) return 0;
var uplst1 = ups[ups.Length - 1];
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
var lstval2 = default(EntityState);
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_fsql.GetEntityString(_entityType, uplst2.Value)}");
var cuig1 = _fsql.CompareEntityValueReturnColumns(_entityType, uplst1.Value, lstval1.Value, true);
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(_entityType, uplst2.Value, lstval2.Value, true) : null;
List<EntityState> data = null;
string[] cuig = null;
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
//最后一个不保存
data = ups.ToList();
data.RemoveAt(ups.Length - 1);
cuig = cuig2;
} else if (isLiveUpdate) {
//立即保存
data = ups.ToList();
cuig = cuig1;
}
if (data?.Count > 0) {
if (cuig.Length == _table.Columns.Count)
return ups.Length == data.Count ? -998 : -997;
var updateSource = data.Select(a => a.Value).ToArray();
var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig);
var affrows = update.ExecuteAffrows();
foreach (var newval in data) {
if (_states.TryGetValue(newval.Key, out var tryold))
_fsql.MapEntityValue(_entityType, newval.Value, tryold.Value);
if (newval.OldValue != null)
_fsql.MapEntityValue(_entityType, newval.Value, newval.OldValue);
}
return affrows;
}
//等待下次对比再保存
return 0;
}
Dictionary<TEntity, byte> _dicUpdateTimes = new Dictionary<TEntity, byte>();
/// <summary>
/// 更新
/// </summary>
/// <param name="data"></param>
public void Update(TEntity data) {
var exists = ExistsInStates(data);
if (exists == null) throw new Exception($"不可更新,未设置主键的值:{_fsql.GetEntityString(_entityType, data)}");
if (exists == false) {
var olddata = OrmSelect(data).First();
if (olddata == null) throw new Exception($"不可更新,数据库不存在该记录:{_fsql.GetEntityString(_entityType, data)}");
}
UpdateRangePriv(new[] { data }, true);
}
public void UpdateRange(IEnumerable<TEntity> data) => UpdateRangePriv(data, true);
void UpdateRangePriv(IEnumerable<TEntity> data, bool isCheck) {
if (CanUpdate(data, true) == false) return;
foreach (var item in data) {
if (_dicUpdateTimes.ContainsKey(item))
DbContextExecCommand();
_dicUpdateTimes.Add(item, 1);
var state = CreateEntityState(item);
state.OldValue = item;
EnqueueToDbContext(DbContext.ExecCommandInfoType.Update, state);
}
if (_ctx.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
AddOrUpdateNavigateList(item);
}
#endregion
#region Remove
int DbContextBetchRemove(EntityState[] dels) {
if (dels.Any() == false) return 0;
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
return Math.Max(dels.Length, affrows);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="data"></param>
public void Remove(TEntity data) => RemoveRange(new[] { data });
public void RemoveRange(IEnumerable<TEntity> data) {
if (CanRemove(data, true) == false) return;
foreach (var item in data) {
var state = CreateEntityState(item);
_states.TryRemove(state.Key, out var trystate);
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(_entityType, item);
EnqueueToDbContext(DbContext.ExecCommandInfoType.Delete, state);
}
}
#endregion
#region AddOrUpdate
/// <summary>
/// 添加或更新
/// </summary>
/// <param name="data"></param>
public void AddOrUpdate(TEntity data) {
if (data == null) throw new ArgumentNullException(nameof(data));
if (_table.Primarys.Any() == false) throw new Exception($"不可添加,实体没有主键:{_fsql.GetEntityString(_entityType, data)}");
var flagExists = ExistsInStates(data);
if (flagExists == false) {
var olddata = OrmSelect(data).First();
if (olddata == null) flagExists = false;
}
if (flagExists == true && CanUpdate(data, false)) {
DbContextExecCommand();
var affrows = _ctx._affrows;
UpdateRangePriv(new[] { data }, false);
DbContextExecCommand();
affrows = _ctx._affrows - affrows;
if (affrows > 0) return;
}
if (CanAdd(data, false)) {
_fsql.ClearEntityPrimaryValueWithIdentity(_entityType, data);
AddPriv(data, false);
}
}
#endregion
}
}

View File

@ -0,0 +1,32 @@
#if ns20
using Microsoft.Extensions.DependencyInjection;
using System;
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;
if (ctx._orm == null)
throw new Exception("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
ctx.InitPropSets();
}
return ctx;
});
return services;
}
}
}
#endif

View File

@ -0,0 +1,38 @@
using FreeSql;
using System;
using System.Collections.Concurrent;
public static class FreeSqlDbContextExtenssions {
/// <summary>
/// 创建普通数据上下文档对象
/// </summary>
/// <param name="that"></param>
/// <returns></returns>
public static DbContext CreateDbContext(this IFreeSql that) {
return new FreeContext(that);
}
/// <summary>
/// 不跟踪查询的实体数据(在不需要更新其数据时使用),可提长查询性能
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="select"></param>
/// <returns></returns>
public static ISelect<T> NoTracking<T>(this ISelect<T> select) where T : class {
return select.TrackToList(null);
}
/// <summary>
/// 设置 DbContext 选项设置
/// </summary>
/// <param name="that"></param>
/// <param name="options"></param>
public static void SetDbContextOptions(this IFreeSql that, Action<DbContextOptions> options) {
if (options == null) return;
var cfg = _dicSetDbContextOptions.GetOrAdd(that, t => new DbContextOptions());
options(cfg);
_dicSetDbContextOptions.AddOrUpdate(that, cfg, (t, o) => cfg);
}
internal static ConcurrentDictionary<IFreeSql, DbContextOptions> _dicSetDbContextOptions = new ConcurrentDictionary<IFreeSql, DbContextOptions>();
}

View File

@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.7.1</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.DbContext</PackageProjectUrl>
<PackageTags>FreeSql ORM DbContext</PackageTags>
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageId>$(AssemblyName)</PackageId>
<Title>$(AssemblyName)</Title>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>FreeSql.DbContext.xml</DocumentationFile>
<WarningLevel>3</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>ns20;netstandard20</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,259 @@
<?xml version="1.0"?>
<doc>
<assembly>
<name>FreeSql.DbContext</name>
</assembly>
<members>
<member name="M:FreeSql.DbContext.Add``1(``0)">
<summary>
添加
</summary>
<typeparam name="TEntity"></typeparam>
<param name="data"></param>
</member>
<member name="M:FreeSql.DbContext.Update``1(``0)">
<summary>
更新
</summary>
<typeparam name="TEntity"></typeparam>
<param name="data"></param>
</member>
<member name="M:FreeSql.DbContext.Remove``1(``0)">
<summary>
删除
</summary>
<typeparam name="TEntity"></typeparam>
<param name="data"></param>
</member>
<member name="M:FreeSql.DbContext.AddOrUpdate``1(``0)">
<summary>
添加或更新
</summary>
<typeparam name="TEntity"></typeparam>
<param name="data"></param>
</member>
<member name="M:FreeSql.DbContext.Attach``1(``0)">
<summary>
附加实体,可用于不查询就更新或删除
</summary>
<typeparam name="TEntity"></typeparam>
<param name="data"></param>
</member>
<member name="P:FreeSql.DbContextOptions.EnableAddOrUpdateNavigateList">
<summary>
是否开启一对多,联级保存功能
</summary>
</member>
<member name="M:FreeSql.DbSet`1.AsType(System.Type)">
<summary>
动态Type在使用 DbSet&lt;object&gt; 后使用本方法,指定实体类型
</summary>
<param name="entityType"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Attach(`0)">
<summary>
附加实体,可用于不查询就更新或删除
</summary>
<param name="data"></param>
</member>
<member name="M:FreeSql.DbSet`1.FlushState">
<summary>
清空状态数据
</summary>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)">
<summary>
添加
</summary>
<param name="data"></param>
</member>
<member name="M:FreeSql.DbSet`1.Update(`0)">
<summary>
更新
</summary>
<param name="data"></param>
</member>
<member name="M:FreeSql.DbSet`1.Remove(`0)">
<summary>
删除
</summary>
<param name="data"></param>
</member>
<member name="M:FreeSql.DbSet`1.AddOrUpdate(`0)">
<summary>
添加或更新
</summary>
<param name="data"></param>
</member>
<member name="M:FreeSql.IRepositoryUnitOfWork.GetRepository``2(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
<summary>
在工作单元内创建默认仓库类,工作单元下的仓储操作具有事务特点
</summary>
<typeparam name="TEntity"></typeparam>
<typeparam name="TKey"></typeparam>
<param name="filter">数据过滤 + 验证</param>
<returns></returns>
</member>
<member name="M:FreeSql.IRepositoryUnitOfWork.GetRepository``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
<summary>
在工作单元内创建联合主键的仓储类,工作单元下的仓储操作具有事务特点
</summary>
<typeparam name="TEntity"></typeparam>
<param name="filter">数据过滤 + 验证</param>
<returns></returns>
</member>
<member name="M:FreeSql.IRepositoryUnitOfWork.GetGuidRepository``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Func{System.String,System.String})">
<summary>
在工作单元内创建仓库类,工作单元下的仓储操作具有事务特点
</summary>
<typeparam name="TEntity"></typeparam>
<param name="filter">数据过滤 + 验证</param>
<param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
<returns></returns>
</member>
<member name="M:FreeSql.IDataFilter`1.Enable(System.String[])">
<summary>
开启过滤器,若使用 using 则使用完后,恢复为原有状态
</summary>
<param name="filterName">过滤器名称</param>
<returns></returns>
</member>
<member name="M:FreeSql.IDataFilter`1.EnableAll">
<summary>
开启所有过滤器,若使用 using 则使用完后,恢复为原有状态
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.IDataFilter`1.Disable(System.String[])">
<summary>
禁用过滤器,若使用 using 则使用完后,恢复为原有状态
</summary>
<param name="filterName"></param>
<returns></returns>
</member>
<member name="M:FreeSql.IDataFilter`1.DisableAll">
<summary>
禁用所有过滤器,若使用 using 则使用完后,恢复为原有状态
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.IBaseRepository.AsType(System.Type)">
<summary>
动态Type在使用 Repository&lt;object&gt; 后使用本方法,指定实体类型
</summary>
<param name="entityType"></param>
<returns></returns>
</member>
<member name="M:FreeSql.IBasicRepository`1.FlushState">
<summary>
清空状态数据
</summary>
</member>
<member name="M:FreeSql.IBasicRepository`1.Attach(`0)">
<summary>
附加实体,可用于不查询就更新或删除
</summary>
<param name="entity"></param>
</member>
<member name="P:FreeSql.IUnitOfWork.Enable">
<summary>
是否启用工作单元
</summary>
</member>
<member name="M:FreeSql.IUnitOfWork.Close">
<summary>
禁用工作单元
<exception cref="T:System.Exception"></exception>
<para></para>
若已开启事务已有Insert/Update/Delete操作调用此方法将发生异常建议在执行逻辑前调用
</summary>
</member>
<member name="M:FreeSql.IUnitOfWork.Open">
<summary>
开启工作单元
</summary>
</member>
<member name="P:FreeSql.UnitOfWork.Enable">
<summary>
是否启用工作单元
</summary>
</member>
<member name="M:FreeSql.UnitOfWork.Close">
<summary>
禁用工作单元
<exception cref="T:System.Exception"></exception>
<para></para>
若已开启事务已有Insert/Update/Delete操作调用此方法将发生异常建议在执行逻辑前调用
</summary>
</member>
<member name="M:FreeSqlDbContextExtenssions.CreateDbContext(IFreeSql)">
<summary>
创建普通数据上下文档对象
</summary>
<param name="that"></param>
<returns></returns>
</member>
<member name="M:FreeSqlDbContextExtenssions.NoTracking``1(FreeSql.ISelect{``0})">
<summary>
不跟踪查询的实体数据(在不需要更新其数据时使用),可提长查询性能
</summary>
<typeparam name="T"></typeparam>
<param name="select"></param>
<returns></returns>
</member>
<member name="M:FreeSqlDbContextExtenssions.SetDbContextOptions(IFreeSql,System.Action{FreeSql.DbContextOptions})">
<summary>
设置 DbContext 选项设置
</summary>
<param name="that"></param>
<param name="options"></param>
</member>
<member name="M:FreeSqlRepositoryExtenssions.GetRepository``2(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
<summary>
返回默认仓库类
</summary>
<typeparam name="TEntity"></typeparam>
<typeparam name="TKey"></typeparam>
<param name="that"></param>
<param name="filter">数据过滤 + 验证</param>
<returns></returns>
</member>
<member name="M:FreeSqlRepositoryExtenssions.GetRepository``1(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
<summary>
返回默认仓库类,适用联合主键的仓储类
</summary>
<typeparam name="TEntity"></typeparam>
<param name="that"></param>
<param name="filter">数据过滤 + 验证</param>
<returns></returns>
</member>
<member name="M:FreeSqlRepositoryExtenssions.GetGuidRepository``1(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Func{System.String,System.String})">
<summary>
返回仓库类
</summary>
<typeparam name="TEntity"></typeparam>
<param name="that"></param>
<param name="filter">数据过滤 + 验证</param>
<param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
<returns></returns>
</member>
<member name="M:FreeSqlRepositoryExtenssions.FromRepository``2(FreeSql.ISelect{``0},FreeSql.BaseRepository{``1})">
<summary>
合并两个仓储的设置(过滤+分表),以便查询
</summary>
<typeparam name="TEntity"></typeparam>
<typeparam name="T2"></typeparam>
<param name="that"></param>
<param name="repos"></param>
<returns></returns>
</member>
<member name="M:FreeSqlRepositoryExtenssions.CreateUnitOfWork(IFreeSql)">
<summary>
创建基于仓储功能的工作单元,务必使用 using 包含使用
</summary>
<param name="that"></param>
<returns></returns>
</member>
</members>
</doc>

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading.Tasks;
namespace FreeSql {
internal class RepositoryDbContext : DbContext {
protected IBaseRepository _repos;
public RepositoryDbContext(IFreeSql orm, IBaseRepository repos) : base() {
_orm = orm;
_repos = repos;
_isUseUnitOfWork = false;
_uowPriv = _repos.UnitOfWork;
}
static ConcurrentDictionary<Type, FieldInfo> _dicGetRepositoryDbField = new ConcurrentDictionary<Type, FieldInfo>();
static FieldInfo GetRepositoryDbField(Type type) => _dicGetRepositoryDbField.GetOrAdd(type, tp => typeof(BaseRepository<,>).MakeGenericType(tp, typeof(int)).GetField("_dbPriv", BindingFlags.Instance | BindingFlags.NonPublic));
public override IDbSet Set(Type entityType) {
if (_dicSet.ContainsKey(entityType)) return _dicSet[entityType];
var tb = _orm.CodeFirst.GetTableByEntity(entityType);
if (tb == null) return null;
object repos = _repos;
if (entityType != _repos.EntityType) {
repos = Activator.CreateInstance(typeof(DefaultRepository<,>).MakeGenericType(entityType, typeof(int)), _repos.Orm);
(repos as IBaseRepository).UnitOfWork = _repos.UnitOfWork;
GetRepositoryDbField(entityType).SetValue(repos, this);
typeof(RepositoryDbContext).GetMethod("SetRepositoryDataFilter").MakeGenericMethod(_repos.EntityType)
.Invoke(null, new object[] { repos, _repos });
}
var sd = Activator.CreateInstance(typeof(RepositoryDbSet<>).MakeGenericType(entityType), repos) as IDbSet;
if (entityType != typeof(object)) _dicSet.Add(entityType, sd);
return sd;
}
public static void SetRepositoryDataFilter<TEntity>(object repos, BaseRepository<TEntity> baseRepo) where TEntity : class {
var filter = baseRepo.DataFilter as DataFilter<TEntity>;
DataFilterUtil.SetRepositoryDataFilter(repos, fl => {
foreach (var f in filter._filters)
fl.Apply<TEntity>(f.Key, f.Value.Expression);
});
}
public override int SaveChanges() {
ExecCommand();
var ret = _affrows;
_affrows = 0;
return ret;
}
async public override Task<int> SaveChangesAsync() {
await ExecCommandAsync();
var ret = _affrows;
_affrows = 0;
return ret;
}
}
}

View File

@ -0,0 +1,60 @@
using FreeSql.Extensions.EntityUtil;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FreeSql {
internal class RepositoryDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
protected BaseRepository<TEntity> _repos;
public RepositoryDbSet(BaseRepository<TEntity> repos) {
_ctx = repos._db;
_fsql = repos.Orm;
_uow = repos.UnitOfWork;
_repos = repos;
}
protected override ISelect<TEntity> OrmSelect(object dywhere) {
var select = base.OrmSelect(dywhere);
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
foreach (var filter in filters) select.Where(filter.Value.Expression);
return select.AsTable(_repos.AsTableSelectInternal);
}
internal ISelect<TEntity> OrmSelectInternal(object dywhere) => OrmSelect(dywhere);
protected override IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys) {
var update = base.OrmUpdate(entitys);
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
foreach (var filter in filters) {
if (entitys != null)
foreach (var entity in entitys)
if (filter.Value.ExpressionDelegate?.Invoke(entity) == false)
throw new Exception($"FreeSql.Repository Update 失败,因为设置了过滤器 {filter.Key}: {filter.Value.Expression},更新的数据不符合 {_fsql.GetEntityString(_entityType, entity)}");
update.Where(filter.Value.Expression);
}
return update.AsTable(_repos.AsTableInternal);
}
internal IUpdate<TEntity> OrmUpdateInternal(IEnumerable<TEntity> entitys) => OrmUpdate(entitys);
protected override IDelete<TEntity> OrmDelete(object dywhere) {
var delete = base.OrmDelete(dywhere);
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
foreach (var filter in filters) delete.Where(filter.Value.Expression);
return delete.AsTable(_repos.AsTableInternal);
}
internal IDelete<TEntity> OrmDeleteInternal(object dywhere) => OrmDelete(dywhere);
protected override IInsert<TEntity> OrmInsert(TEntity entity) => OrmInsert(new[] { entity });
protected override IInsert<TEntity> OrmInsert(IEnumerable<TEntity> entitys) {
var insert = base.OrmInsert(entitys);
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
foreach (var filter in filters) {
if (entitys != null)
foreach (var entity in entitys)
if (filter.Value.ExpressionDelegate?.Invoke(entity) == false)
throw new Exception($"FreeSql.Repository Insert 失败,因为设置了过滤器 {filter.Key}: {filter.Value.Expression},插入的数据不符合 {_fsql.GetEntityString(_entityType, entity)}");
}
return insert.AsTable(_repos.AsTableInternal);
}
internal IInsert<TEntity> OrmInsertInternal(TEntity entity) => OrmInsert(entity);
internal IInsert<TEntity> OrmInsertInternal(IEnumerable<TEntity> entitys) => OrmInsert(entitys);
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Linq.Expressions;
namespace FreeSql {
public interface IRepositoryUnitOfWork : IUnitOfWork {
/// <summary>
/// 在工作单元内创建默认仓库类,工作单元下的仓储操作具有事务特点
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
/// <summary>
/// 在工作单元内创建联合主键的仓储类,工作单元下的仓储操作具有事务特点
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
BaseRepository<TEntity> GetRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
/// <summary>
/// 在工作单元内创建仓库类,工作单元下的仓储操作具有事务特点
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="filter">数据过滤 + 验证</param>
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
/// <returns></returns>
GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class;
}
class RepositoryUnitOfWork : UnitOfWork, IRepositoryUnitOfWork {
public RepositoryUnitOfWork(IFreeSql fsql) : base(fsql) {
}
public GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
var repos = new GuidRepository<TEntity>(_fsql, filter, asTable);
repos.UnitOfWork = this;
return repos;
}
public DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
var repos = new DefaultRepository<TEntity, TKey>(_fsql, filter);
repos.UnitOfWork = this;
return repos;
}
public BaseRepository<TEntity> GetRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
var repos = new DefaultRepository<TEntity, int>(_fsql, filter);
repos.UnitOfWork = this;
return repos;
}
}
}

View File

@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Linq;
namespace FreeSql {
public interface IDataFilter<TEntity> : IDisposable where TEntity : class {
IDataFilter<TEntity> Apply(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp);
/// <summary>
/// 开启过滤器,若使用 using 则使用完后,恢复为原有状态
/// </summary>
/// <param name="filterName">过滤器名称</param>
/// <returns></returns>
IDisposable Enable(params string[] filterName);
/// <summary>
/// 开启所有过滤器,若使用 using 则使用完后,恢复为原有状态
/// </summary>
/// <returns></returns>
IDisposable EnableAll();
/// <summary>
/// 禁用过滤器,若使用 using 则使用完后,恢复为原有状态
/// </summary>
/// <param name="filterName"></param>
/// <returns></returns>
IDisposable Disable(params string[] filterName);
/// <summary>
/// 禁用所有过滤器,若使用 using 则使用完后,恢复为原有状态
/// </summary>
/// <returns></returns>
IDisposable DisableAll();
bool IsEnabled(string filterName);
}
internal class DataFilter<TEntity> : IDataFilter<TEntity> where TEntity : class {
internal class FilterItem {
public Expression<Func<TEntity, bool>> Expression { get; set; }
Func<TEntity, bool> _expressionDelegate;
public Func<TEntity, bool> ExpressionDelegate => _expressionDelegate ?? (_expressionDelegate = Expression?.Compile());
public bool IsEnabled { get; set; }
}
internal ConcurrentDictionary<string, FilterItem> _filters = new ConcurrentDictionary<string, FilterItem>(StringComparer.CurrentCultureIgnoreCase);
public IDataFilter<TEntity> Apply(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp) {
if (filterName == null)
throw new ArgumentNullException(nameof(filterName));
if (filterAndValidateExp == null) return this;
var filterItem = new FilterItem { Expression = filterAndValidateExp, IsEnabled = true };
_filters.AddOrUpdate(filterName, filterItem, (k, v) => filterItem);
return this;
}
public IDisposable Disable(params string[] filterName) {
if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });
List<string> restore = new List<string>();
foreach (var name in filterName) {
if (_filters.TryGetValue(name, out var tryfi)) {
if (tryfi.IsEnabled) {
restore.Add(name);
tryfi.IsEnabled = false;
}
}
}
return new UsingAny(() => this.Enable(restore.ToArray()));
}
public IDisposable DisableAll() {
List<string> restore = new List<string>();
foreach (var val in _filters) {
if (val.Value.IsEnabled) {
restore.Add(val.Key);
val.Value.IsEnabled = false;
}
}
return new UsingAny(() => this.Enable(restore.ToArray()));
}
class UsingAny : IDisposable {
Action _ondis;
public UsingAny(Action ondis) {
_ondis = ondis;
}
public void Dispose() {
_ondis?.Invoke();
}
}
public IDisposable Enable(params string[] filterName) {
if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });
List<string> restore = new List<string>();
foreach (var name in filterName) {
if (_filters.TryGetValue(name, out var tryfi)) {
if (tryfi.IsEnabled == false) {
restore.Add(name);
tryfi.IsEnabled = true;
}
}
}
return new UsingAny(() => this.Disable(restore.ToArray()));
}
public IDisposable EnableAll() {
List<string> restore = new List<string>();
foreach (var val in _filters) {
if (val.Value.IsEnabled == false) {
restore.Add(val.Key);
val.Value.IsEnabled = true;
}
}
return new UsingAny(() => this.Disable(restore.ToArray()));
}
public bool IsEnabled(string filterName) {
if (filterName == null) return false;
return _filters.TryGetValue(filterName, out var tryfi) ? tryfi.IsEnabled : false;
}
~DataFilter() {
this.Dispose();
}
public void Dispose() {
_filters.Clear();
}
}
public class FluentDataFilter : IDisposable {
internal List<(Type type, string name, LambdaExpression exp)> _filters = new List<(Type type, string name, LambdaExpression exp)>();
public FluentDataFilter Apply<TEntity>(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp) where TEntity : class {
if (filterName == null)
throw new ArgumentNullException(nameof(filterName));
if (filterAndValidateExp == null) return this;
_filters.Add((typeof(TEntity), filterName, filterAndValidateExp));
return this;
}
~FluentDataFilter() {
this.Dispose();
}
public void Dispose() {
_filters.Clear();
}
}
}

View File

@ -0,0 +1,89 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace FreeSql {
internal class DataFilterUtil {
internal static Action<FluentDataFilter> _globalDataFilter;
static ConcurrentDictionary<Type, Delegate> _dicSetRepositoryDataFilterApplyDataFilterFunc = new ConcurrentDictionary<Type, Delegate>();
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicSetRepositoryDataFilterConvertFilterNotExists = new ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>();
internal static void SetRepositoryDataFilter(object repos, Action<FluentDataFilter> scopedDataFilter) {
if (scopedDataFilter != null) {
SetRepositoryDataFilter(repos, null);
}
if (scopedDataFilter == null) {
scopedDataFilter = _globalDataFilter;
}
if (scopedDataFilter == null) return;
using (var globalFilter = new FluentDataFilter()) {
scopedDataFilter(globalFilter);
var type = repos.GetType();
Type entityType = (repos as IBaseRepository).EntityType;
if (entityType == null) throw new Exception("FreeSql.Repository 设置过滤器失败,原因是对象不属于 IRepository");
var notExists = _dicSetRepositoryDataFilterConvertFilterNotExists.GetOrAdd(type, t => new ConcurrentDictionary<string, bool>());
var newFilter = new Dictionary<string, LambdaExpression>();
foreach (var gf in globalFilter._filters) {
if (notExists.ContainsKey(gf.name)) continue;
LambdaExpression newExp = null;
var filterParameter1 = Expression.Parameter(entityType, gf.exp.Parameters[0].Name);
try {
newExp = Expression.Lambda(
typeof(Func<,>).MakeGenericType(entityType, typeof(bool)),
new ReplaceVisitor().Modify(gf.exp.Body, filterParameter1),
filterParameter1
);
} catch {
notExists.TryAdd(gf.name, true); //防止第二次错误
continue;
}
newFilter.Add(gf.name, newExp);
}
if (newFilter.Any() == false) return;
var del = _dicSetRepositoryDataFilterApplyDataFilterFunc.GetOrAdd(type, t => {
var reposParameter = Expression.Parameter(type);
var nameParameter = Expression.Parameter(typeof(string));
var expressionParameter = Expression.Parameter(
typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(entityType, typeof(bool)))
);
return Expression.Lambda(
Expression.Block(
Expression.Call(reposParameter, type.GetMethod("ApplyDataFilter", BindingFlags.Instance | BindingFlags.NonPublic), nameParameter, expressionParameter)
),
new[] {
reposParameter, nameParameter, expressionParameter
}
).Compile();
});
foreach (var nf in newFilter) {
del.DynamicInvoke(repos, nf.Key, nf.Value);
}
newFilter.Clear();
}
}
}
class ReplaceVisitor : ExpressionVisitor {
private ParameterExpression parameter;
public Expression Modify(Expression expression, ParameterExpression parameter) {
this.parameter = parameter;
return Visit(expression);
}
protected override Expression VisitMember(MemberExpression node) {
if (node.Expression?.NodeType == ExpressionType.Parameter)
return Expression.Property(parameter, node.Member.Name);
return base.VisitMember(node);
}
}
}

View File

@ -0,0 +1,39 @@
#if ns20
using System;
using System.Reflection;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
namespace FreeSql {
public static class FreeSqlRepositoryDependencyInjection {
public static IServiceCollection AddFreeRepository(this IServiceCollection services, Action<FluentDataFilter> globalDataFilter = null, params Assembly[] assemblies) {
DataFilterUtil._globalDataFilter = globalDataFilter;
services.AddScoped(typeof(IReadOnlyRepository<>), typeof(GuidRepository<>));
services.AddScoped(typeof(IBasicRepository<>), typeof(GuidRepository<>));
services.AddScoped(typeof(BaseRepository<>), typeof(GuidRepository<>));
services.AddScoped(typeof(GuidRepository<>));
services.AddScoped(typeof(IReadOnlyRepository<,>), typeof(DefaultRepository<,>));
services.AddScoped(typeof(IBasicRepository<,>), typeof(DefaultRepository<,>));
services.AddScoped(typeof(BaseRepository<,>), typeof(DefaultRepository<,>));
services.AddScoped(typeof(DefaultRepository<,>));
if (assemblies?.Any() == true) {
foreach(var asse in assemblies) {
foreach (var repos in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a))) {
services.AddScoped(repos);
}
}
}
return services;
}
}
}
#endif

View File

@ -0,0 +1,65 @@
using FreeSql;
using System;
using System.Linq.Expressions;
using System.Linq;
public static class FreeSqlRepositoryExtenssions {
/// <summary>
/// 返回默认仓库类
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="that"></param>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
return new DefaultRepository<TEntity, TKey>(that, filter);
}
/// <summary>
/// 返回默认仓库类,适用联合主键的仓储类
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="that"></param>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
public static BaseRepository<TEntity> GetRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
return new DefaultRepository<TEntity, int>(that, filter);
}
/// <summary>
/// 返回仓库类
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="that"></param>
/// <param name="filter">数据过滤 + 验证</param>
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
/// <returns></returns>
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
return new GuidRepository<TEntity>(that, filter, asTable);
}
/// <summary>
/// 合并两个仓储的设置(过滤+分表),以便查询
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="that"></param>
/// <param name="repos"></param>
/// <returns></returns>
public static ISelect<TEntity> FromRepository<TEntity, T2>(this ISelect<TEntity> that, BaseRepository<T2> repos) where TEntity : class where T2 : class {
var filters = (repos.DataFilter as DataFilter<T2>)._filters.Where(a => a.Value.IsEnabled == true);
foreach (var filter in filters) that.Where<T2>(filter.Value.Expression);
return that.AsTable(repos.AsTableSelectInternal);
}
/// <summary>
/// 创建基于仓储功能的工作单元,务必使用 using 包含使用
/// </summary>
/// <param name="that"></param>
/// <returns></returns>
public static IRepositoryUnitOfWork CreateUnitOfWork(this IFreeSql that) {
return new RepositoryUnitOfWork(that);
}
}

View File

@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace FreeSql {
public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity>
where TEntity : class {
internal RepositoryDbContext _dbPriv;
internal RepositoryDbContext _db => _dbPriv ?? (_dbPriv = new RepositoryDbContext(Orm, this));
internal RepositoryDbSet<TEntity> _dbsetPriv;
internal RepositoryDbSet<TEntity> _dbset => _dbsetPriv ?? (_dbsetPriv = _db.Set<TEntity>() as RepositoryDbSet<TEntity>);
public IDataFilter<TEntity> DataFilter { get; } = new DataFilter<TEntity>();
Func<string, string> _asTableVal;
protected Func<string, string> AsTable {
get => _asTableVal;
set {
_asTableVal = value;
AsTableSelect = value == null ? null : new Func<Type, string, string>((a, b) => a == EntityType ? value(b) : null);
}
}
internal Func<string, string> AsTableInternal => AsTable;
protected Func<Type, string, string> AsTableSelect { get; private set; }
internal Func<Type, string, string> AsTableSelectInternal => AsTableSelect;
protected void ApplyDataFilter(string name, Expression<Func<TEntity, bool>> exp) => DataFilter.Apply(name, exp);
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) {
Orm = fsql;
DataFilterUtil.SetRepositoryDataFilter(this, null);
DataFilter.Apply("", filter);
AsTable = asTable;
}
~BaseRepository() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
try {
_dbsetPriv?.Dispose();
_dbPriv?.Dispose();
this.DataFilter.Dispose();
} finally {
_isdisposed = true;
GC.SuppressFinalize(this);
}
}
public Type EntityType => _dbsetPriv?.EntityType ?? typeof(TEntity);
public void AsType(Type entityType) => _dbset.AsType(entityType);
public IFreeSql Orm { get; private set; }
public IUnitOfWork UnitOfWork { get; set; }
public IUpdate<TEntity> UpdateDiy => _dbset.OrmUpdateInternal(null);
public ISelect<TEntity> Select => _dbset.OrmSelectInternal(null);
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => _dbset.OrmSelectInternal(null).Where(exp);
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => _dbset.OrmSelectInternal(null).WhereIf(condition, exp);
public int Delete(Expression<Func<TEntity, bool>> predicate) => _dbset.OrmDeleteInternal(null).Where(predicate).ExecuteAffrows();
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _dbset.OrmDeleteInternal(null).Where(predicate).ExecuteAffrowsAsync();
public int Delete(TEntity entity) {
_dbset.Remove(entity);
return _db.SaveChanges();
}
public Task<int> DeleteAsync(TEntity entity) {
_dbset.Remove(entity);
return _db.SaveChangesAsync();
}
public int Delete(IEnumerable<TEntity> entitys) {
_dbset.RemoveRange(entitys);
return _db.SaveChanges();
}
public Task<int> DeleteAsync(IEnumerable<TEntity> entitys) {
_dbset.RemoveRange(entitys);
return _db.SaveChangesAsync();
}
public virtual TEntity Insert(TEntity entity) {
_dbset.Add(entity);
_db.SaveChanges();
return entity;
}
async public virtual Task<TEntity> InsertAsync(TEntity entity) {
await _dbset.AddAsync(entity);
_db.SaveChanges();
return entity;
}
public virtual List<TEntity> Insert(IEnumerable<TEntity> entitys) {
_dbset.AddRange(entitys);
_db.SaveChanges();
return entitys.ToList();
}
async public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys) {
await _dbset.AddRangeAsync(entitys);
await _db.SaveChangesAsync();
return entitys.ToList();
}
public int Update(TEntity entity) {
_dbset.Update(entity);
return _db.SaveChanges();
}
public Task<int> UpdateAsync(TEntity entity) {
_dbset.Update(entity);
return _db.SaveChangesAsync();
}
public int Update(IEnumerable<TEntity> entitys) {
_dbset.UpdateRange(entitys);
return _db.SaveChanges();
}
public Task<int> UpdateAsync(IEnumerable<TEntity> entitys) {
_dbset.UpdateRange(entitys);
return _db.SaveChangesAsync();
}
public void Attach(TEntity data) => _db.Attach(data);
public void Attach(IEnumerable<TEntity> data) => _db.AttachRange(data);
public void FlushState() => _dbset.FlushState();
public TEntity InsertOrUpdate(TEntity entity) {
_dbset.AddOrUpdate(entity);
_db.SaveChanges();
return entity;
}
async public Task<TEntity> InsertOrUpdateAsync(TEntity entity) {
await _dbset.AddOrUpdateAsync(entity);
_db.SaveChanges();
return entity;
}
}
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IBaseRepository<TEntity, TKey>
where TEntity : class {
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) {
}
public int Delete(TKey id) {
var stateKey = string.Concat(id);
_dbset._statesInternal.TryRemove(stateKey, out var trystate);
return _dbset.OrmDeleteInternal(id).ExecuteAffrows();
}
public Task<int> DeleteAsync(TKey id) {
var stateKey = string.Concat(id);
_dbset._statesInternal.TryRemove(stateKey, out var trystate);
return _dbset.OrmDeleteInternal(id).ExecuteAffrowsAsync();
}
public TEntity Find(TKey id) => _dbset.OrmSelectInternal(id).ToOne();
public Task<TEntity> FindAsync(TKey id) => _dbset.OrmSelectInternal(id).ToOneAsync();
public TEntity Get(TKey id) => Find(id);
public Task<TEntity> GetAsync(TKey id) => FindAsync(id);
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Linq.Expressions;
namespace FreeSql {
public class DefaultRepository<TEntity, TKey> :
BaseRepository<TEntity, TKey>
where TEntity : class {
public DefaultRepository(IFreeSql fsql) : base(fsql, null, null) {
}
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null) {
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Linq.Expressions;
namespace FreeSql {
public class GuidRepository<TEntity> :
BaseRepository<TEntity, Guid>
where TEntity : class {
public GuidRepository(IFreeSql fsql) : this(fsql, null, null) {
}
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) : base(fsql, filter, asTable) {
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace FreeSql {
public interface IBaseRepository : IDisposable {
Type EntityType { get; }
IUnitOfWork UnitOfWork { get; set; }
IFreeSql Orm { get; }
/// <summary>
/// 动态Type在使用 Repository&lt;object&gt; 后使用本方法,指定实体类型
/// </summary>
/// <param name="entityType"></param>
/// <returns></returns>
void AsType(Type entityType);
}
public interface IBaseRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity>
where TEntity : class {
int Delete(Expression<Func<TEntity, bool>> predicate);
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate);
}
public interface IBaseRepository<TEntity, TKey> : IBaseRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>, IBasicRepository<TEntity, TKey>
where TEntity : class {
}
}

View File

@ -0,0 +1,45 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace FreeSql {
public interface IBasicRepository<TEntity> : IReadOnlyRepository<TEntity>
where TEntity : class {
TEntity Insert(TEntity entity);
List<TEntity> Insert(IEnumerable<TEntity> entitys);
Task<TEntity> InsertAsync(TEntity entity);
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys);
/// <summary>
/// 清空状态数据
/// </summary>
void FlushState();
/// <summary>
/// 附加实体,可用于不查询就更新或删除
/// </summary>
/// <param name="entity"></param>
void Attach(TEntity entity);
void Attach(IEnumerable<TEntity> entity);
int Update(TEntity entity);
int Update(IEnumerable<TEntity> entitys);
Task<int> UpdateAsync(TEntity entity);
Task<int> UpdateAsync(IEnumerable<TEntity> entitys);
TEntity InsertOrUpdate(TEntity entity);
Task<TEntity> InsertOrUpdateAsync(TEntity entity);
IUpdate<TEntity> UpdateDiy { get; }
int Delete(TEntity entity);
int Delete(IEnumerable<TEntity> entitys);
Task<int> DeleteAsync(TEntity entity);
Task<int> DeleteAsync(IEnumerable<TEntity> entitys);
}
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>
where TEntity : class {
int Delete(TKey id);
Task<int> DeleteAsync(TKey id);
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace FreeSql {
public interface IReadOnlyRepository<TEntity> : IBaseRepository
where TEntity : class {
IDataFilter<TEntity> DataFilter { get; }
ISelect<TEntity> Select { get; }
ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp);
ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp);
}
public interface IReadOnlyRepository<TEntity, TKey> : IReadOnlyRepository<TEntity>
where TEntity : class {
TEntity Get(TKey id);
Task<TEntity> GetAsync(TKey id);
TEntity Find(TKey id);
Task<TEntity> FindAsync(TKey id);
}
}

View File

@ -0,0 +1,5 @@

namespace FreeSql.Extensions.EntityUtil {
public static class TempExtensions {
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Data;
using System.Data.Common;
namespace FreeSql {
public interface IUnitOfWork : IDisposable {
DbTransaction GetOrBeginTransaction(bool isCreate = true);
IsolationLevel? IsolationLevel { get; set; }
/// <summary>
/// 是否启用工作单元
/// </summary>
bool Enable { get; }
void Commit();
void Rollback();
/// <summary>
/// 禁用工作单元
/// <exception cref="Exception"></exception>
/// <para></para>
/// 若已开启事务已有Insert/Update/Delete操作调用此方法将发生异常建议在执行逻辑前调用
/// </summary>
void Close();
/// <summary>
/// 开启工作单元
/// </summary>
void Open();
}
}

View File

@ -0,0 +1,103 @@
using SafeObjectPool;
using System;
using System.Data;
using System.Data.Common;
namespace FreeSql {
class UnitOfWork : IUnitOfWork {
protected IFreeSql _fsql;
protected Object<DbConnection> _conn;
protected DbTransaction _tran;
public UnitOfWork(IFreeSql fsql) {
_fsql = fsql;
}
void ReturnObject() {
_fsql.Ado.MasterPool.Return(_conn);
_tran = null;
_conn = null;
}
/// <summary>
/// 是否启用工作单元
/// </summary>
public bool Enable { get; private set; } = true;
/// <summary>
/// 禁用工作单元
/// <exception cref="Exception"></exception>
/// <para></para>
/// 若已开启事务已有Insert/Update/Delete操作调用此方法将发生异常建议在执行逻辑前调用
/// </summary>
public void Close()
{
if (_tran != null)
{
throw new Exception("已开启事务,不能禁用工作单元");
}
Enable = false;
}
public void Open()
{
Enable = true;
}
public IsolationLevel? IsolationLevel { get; set; }
public DbTransaction GetOrBeginTransaction(bool isCreate = true) {
if (_tran != null) return _tran;
if (isCreate == false) return null;
if (!Enable) return null;
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
_conn = _fsql.Ado.MasterPool.Get();
try {
_tran = IsolationLevel == null ?
_conn.Value.BeginTransaction() :
_conn.Value.BeginTransaction(IsolationLevel.Value);
} catch {
ReturnObject();
throw;
}
return _tran;
}
public void Commit() {
if (_tran != null) {
try {
_tran.Commit();
} finally {
ReturnObject();
}
}
}
public void Rollback() {
if (_tran != null) {
try {
_tran.Rollback();
} finally {
ReturnObject();
}
}
}
~UnitOfWork() {
this.Dispose();
}
bool _isdisposed = false;
public void Dispose() {
if (_isdisposed) return;
try {
this.Rollback();
} finally {
_isdisposed = true;
GC.SuppressFinalize(this);
}
}
}
}

284
FreeSql.DbContext/readme.md Normal file
View File

@ -0,0 +1,284 @@
这是 [FreeSql](https://github.com/2881099/FreeSql) 衍生出来的扩展包,包含 DbContext & DbSet、Repository & UnitOfWork 实现面向对象的特性QQ群4336577
> dotnet add package FreeSql.DbContext
## 更新日志
### v0.6.5
- 修复 Repository 联级保存的 bug
- 添加工作单元开启方法;
- 适配 .net framework 4.5、netstandard 2.0
### v0.6.1
- 拆分 FreeSql 小包引用,各数据库单独包、延时加载包;
- FreeSql.Extensions.LazyLoading
- FreeSql.Provider.MySql
- FreeSql.Provider.PostgreSQL
- FreeSql.Provider.SqlServer
- FreeSql.Provider.Sqlite
- FreeSql.Provider.Oracle
- 移除 IFreeSql.Cache以及 ISelect.Caching 方法;
- 移除 IFreeSql.Log包括内部原有的日志输出改为 Trace.WriteLine
- IAdo.Query\<dynamic\> 读取返回变为 List\<Dictionary\<string, object\>\>
- 定义 IFreeSql 和以前一样,移除了 UseCache、UseLogger 方法;
## DbContext & DbSet
```csharp
using (var ctx = new SongContext()) {
var song = new Song { BigNumber = "1000000000000000000" };
ctx.Songs.Add(song);
song.BigNumber = (BigInteger.Parse(song.BigNumber) + 1).ToString();
ctx.Songs.Update(song);
var tag = new Tag {
Name = "testaddsublist",
Tags = new[] {
new Tag { Name = "sub1" },
new Tag { Name = "sub2" },
new Tag {
Name = "sub3",
Tags = new[] {
new Tag { Name = "sub3_01" }
}
}
}
};
ctx.Tags.Add(tag);
ctx.SaveChanges();
}
```
## Repository & UnitOfWork
仓储与工作单元一起使用,工作单元具有事务特点。
```csharp
using (var unitOfWork = fsql.CreateUnitOfWork()) {
var songRepository = unitOfWork.GetRepository<Song, int>();
var tagRepository = unitOfWork.GetRepository<Tag, int>();
var song = new Song { BigNumber = "1000000000000000000" };
songRepository.Insert(song);
songRepository.Update(song);
song.BigNumber = (BigInteger.Parse(song.BigNumber) + 1).ToString();
songRepository.Update(song);
var tag = new Tag {
Name = "testaddsublist",
Tags = new[] {
new Tag { Name = "sub1" },
new Tag { Name = "sub2" },
new Tag {
Name = "sub3",
Tags = new[] {
new Tag { Name = "sub3_01" }
}
}
}
};
tagRepository.Insert(tag);
ctx.Commit();
}
```
## Repository
简单使用仓储,有状态跟踪,它不包含事务的特点。
```csharp
var songRepository = fsql.GetRepository<Song, int>();
var song = new Song { BigNumber = "1000000000000000000" };
songRepository.Insert(song);
```
## IFreeSql 核心定义
```csharp
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\dd2.db;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
.Build();
public class Song {
[Column(IsIdentity = true)]
public int Id { get; set; }
public string BigNumber { get; set; }
[Column(IsVersion = true)] //乐观锁
public long versionRow { 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 string Name { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class SongContext : DbContext {
public DbSet<Song> Songs { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
builder.UseFreeSql(fsql);
}
}
```
# 过滤器与验证
假设我们有User(用户)、Topic(主题)两个实体,在领域类中定义了两个仓储:
```csharp
var userRepository = fsql.GetGuidRepository<User>();
var topicRepository = fsql.GetGuidRepository<Topic>();
```
在开发过程中,总是担心 topicRepository 的数据安全问题即有可能查询或操作到其他用户的主题。因此我们在v0.0.7版本进行了改进,增加了 filter lambad 表达式参数。
```csharp
var userRepository = fsql.GetGuidRepository<User>(a => a.Id == 1);
var topicRepository = fsql.GetGuidRepository<Topic>(a => a.UserId == 1);
```
* 在查询/修改/删除时附加此条件,从而达到不会修改其他用户的数据;
* 在添加时,使用表达式验证数据的合法性,若不合法则抛出异常;
# 分表与分库
FreeSql 提供 AsTable 分表的基础方法GuidRepository 作为分存式仓储将实现了分表与分库(不支持跨服务器分库)的封装。
```csharp
var logRepository = fsql.GetGuidRepository<Log>(null, oldname => $"{oldname}_{DateTime.Now.ToString("YYYYMM")}");
```
上面我们得到一个日志仓储按年月分表,使用它 CURD 最终会操作 Log_201903 表。
合并两个仓储,实现分表下的联表查询:
```csharp
fsql.GetGuidRepository<User>().Select.FromRepository(logRepository)
.LeftJoin<Log>(b => b.UserId == a.Id)
.ToList();
```
注意事项:
* 不能使用 CodeFirst 迁移分表,开发环境时仍然可以迁移 Log 表;
* 不可在分表分库的实体类型中使用《延时加载》;
# 历史版本
### v0.5.23
- 增加 DbSet/Repository FlushState 手工清除状态管理数据;
### v0.5.21
- 修复 AddOrUpdate/InsertOrUpdate 当主键无值时,仍然查询了一次数据库;
- 增加 查询数据时 TrackToList 对导航集合的状态跟踪;
- 完善 AddOrUpdateNavigateList 联级保存,忽略标记 IsIgnore 的集合属性;
- 完成 IFreeSql.Include、IncludeMany 功能;
### v0.5.12
- 增加 工作单元开关,可在任意 Insert/Update/Delete 之前调用,以关闭工作单元使其失效;[PR #1](https://github.com/2881099/FreeSql.DbContext/pull/1)
### v0.5.9
- 增加 linq to sql 的查询语法,以及单元测试,[wiki](https://github.com/2881099/FreeSql/wiki/LinqToSql)
- 修复 EnableAddOrUpdateNavigateList 设置对异步方法无效的 bug
### v0.5.8
- 增加 IFreeSql.SetDbContextOptions 设置 DbContext 的功能开启或禁用连级一对多导航集合属性保存的功能EnableAddOrUpdateNavigateList默认开启
- 增加 IUnitOfWork.IsolationLevel 设置事务级别;
### v0.5.7
- 修复 UnitOfWork.GetRepository() 事务 bug原因仓储的每步操作都提交了事务
### v0.5.5
- 修复 MapEntityValue 对 IsIgnore 未处理的 bug
### v0.5.4
- 修复 Repository 追加导航集合的保存 bug
- 公开 IRepository.Orm 对象;
### v0.5.3
- 修复 实体跟踪的 bug当查询到的实体自增值为 0 时重现;
- 优化 状态管理字典为 ConcurrentDictionary
### v0.5.2
- 优化 SqlServer UnitOfWork 使用bug在 FreeSql 内部解决的;
- 补充 测试与支持联合主键的自增;
### v0.5.1
- 补充 开放 DbContext.UnitOfWork 对象,方便扩展并保持在同一个事务执行;
- 补充 增加 DbSet\<object\>、Repository\<object\> 使用方法,配合 AsType(实体类型),实现弱类型操作;
- 修复 DbContext.AddOrUpdate 传入 null 时,任然会查询一次数据库的 bug
- 优化 DbContext.AddOrUpdate 未添加实体主键的错误提醒;
- 修复 DbContext.Set\<object\> 缓存的 bug使用多种弱类型时发生
- 修复 IsIgnore 过滤字段后,查询的错误;
- 修复 全局过滤器功能迁移的遗留 bug
### v0.4.14
- 优化 Add 时未设置主键的错误提醒;
### v0.4.13
- 补充 Repository 增加 Attach 方法;
- 优化 Update/AddOrUpdate 实体的时候,若状态管理不存在,尝试查询一次数据库,以便跟踪对象;
### v0.4.12
- 修复 非自增情况下Add 后再 Update 该实体时,错误(需要先 Attach 或查询)的 bug
### v0.4.10
- 补充 开放 DbContext.Orm 对象;
- 修复 OnConfiguring 未配置时注入获取失败的 bug
### v0.4.6
- 修复 DbSet AddRange/UpdateRange/RemoveRange 参数为空列表时报错,现在不用判断 data.Any() == true 再执行;
- 增加 DbContext 对 DbSet 的快速代理方法(Add/Update/Remove/Attach)
- 增加 DbContext 通用类命名为FreeContext也可以通过 IFreeSql 扩展方法 CreateDbContext 创建;
- 增加 ISelect NoTracking 扩展方法,查询数据时不追踪(从而提升查询性能);
### v0.4.5
- 增加 DbSet Attach 方法附加实体,可用于不查询就更新或删除;
### v0.4.2
- 增加 DbSet UpdateAsync/UpdateRangeAsync 方法,当一个实体被更新两次时,会先执行前面的队列;
- 增加 GetRepository 获取联合主键的适用仓储类;
- 增加 DbSet 在 Add/Update 时对导航属性(OneToMany) 的处理AddOrUpdate
### v0.4.1
- 独立 FreeSql.DbContext 项目;
- 实现 Repository + DbSet 统一的状态跟踪与工作单元;
- 增加 DbSet AddOrUpdate 方法;
- 增加 Repository InsertOrUpdate 方法;

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<Version>0.7.1</Version>
<Authors>YeXiangQin</Authors>
<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>
<PackageTags>FreeSql ORM Repository</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageId>$(AssemblyName)</PackageId>
<Title>$(AssemblyName)</Title>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,132 @@
这是 [FreeSql](https://github.com/2881099/FreeSql) 衍生出来的扩展包,包含 Repository & UnitOfWork 实现面向对象的特性QQ群4336577
> dotnet add package FreeSql.Repository
## Repository & UnitOfWork
仓储与工作单元一起使用,工作单元具有事务特点。
```csharp
using (var unitOfWork = fsql.CreateUnitOfWork()) {
var songRepository = unitOfWork.GetRepository<Song, int>();
var tagRepository = unitOfWork.GetRepository<Tag, int>();
var song = new Song { BigNumber = "1000000000000000000" };
songRepository.Insert(song);
songRepository.Update(song);
song.BigNumber = (BigInteger.Parse(song.BigNumber) + 1).ToString();
songRepository.Update(song);
var tag = new Tag {
Name = "testaddsublist",
Tags = new[] {
new Tag { Name = "sub1" },
new Tag { Name = "sub2" },
new Tag {
Name = "sub3",
Tags = new[] {
new Tag { Name = "sub3_01" }
}
}
}
};
tagRepository.Insert(tag);
ctx.Commit();
}
```
## Repository
简单使用仓储,有状态跟踪,它不包含事务的特点。
```csharp
var songRepository = fsql.GetRepository<Song, int>();
var song = new Song { BigNumber = "1000000000000000000" };
songRepository.Insert(song);
```
## IFreeSql 核心定义
```csharp
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\dd2.db;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
.Build();
public class Song {
[Column(IsIdentity = true)]
public int Id { get; set; }
public string BigNumber { get; set; }
[Column(IsVersion = true)] //乐观锁
public long versionRow { 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 string Name { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class SongContext : DbContext {
public DbSet<Song> Songs { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
builder.UseFreeSql(fsql);
}
}
```
# 过滤器与验证
假设我们有User(用户)、Topic(主题)两个实体,在领域类中定义了两个仓储:
```csharp
var userRepository = fsql.GetGuidRepository<User>();
var topicRepository = fsql.GetGuidRepository<Topic>();
```
在开发过程中,总是担心 topicRepository 的数据安全问题即有可能查询或操作到其他用户的主题。因此我们在v0.0.7版本进行了改进,增加了 filter lambad 表达式参数。
```csharp
var userRepository = fsql.GetGuidRepository<User>(a => a.Id == 1);
var topicRepository = fsql.GetGuidRepository<Topic>(a => a.UserId == 1);
```
* 在查询/修改/删除时附加此条件,从而达到不会修改其他用户的数据;
* 在添加时,使用表达式验证数据的合法性,若不合法则抛出异常;
# 分表与分库
FreeSql 提供 AsTable 分表的基础方法GuidRepository 作为分存式仓储将实现了分表与分库(不支持跨服务器分库)的封装。
```csharp
var logRepository = fsql.GetGuidRepository<Log>(null, oldname => $"{oldname}_{DateTime.Now.ToString("YYYYMM")}");
```
上面我们得到一个日志仓储按年月分表,使用它 CURD 最终会操作 Log_201903 表。
合并两个仓储,实现分表下的联表查询:
```csharp
fsql.GetGuidRepository<User>().Select.FromRepository(logRepository)
.LeftJoin<Log>(b => b.UserId == a.Id)
.ToList();
```
注意事项:
* 不能使用 CodeFirst 迁移分表,开发环境时仍然可以迁移 Log 表;
* 不可在分表分库的实体类型中使用《延时加载》;

View File

@ -1,26 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.MySql\FreeSql.Provider.MySql.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.Oracle\FreeSql.Provider.Oracle.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.PostgreSQL\FreeSql.Provider.PostgreSQL.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.SqlServer\FreeSql.Provider.SqlServer.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.MySql\FreeSql.Provider.MySql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Oracle\FreeSql.Provider.Oracle.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.PostgreSQL\FreeSql.Provider.PostgreSQL.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.SqlServer\FreeSql.Provider.SqlServer.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,261 @@
using System;
using Xunit;
namespace FreeSql.Tests {
public class RepositoryTests {
[Fact]
public void AddUpdate() {
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
var item = repos.Insert(new AddUpdateInfo());
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
item = repos.Insert(new AddUpdateInfo { Id = Guid.NewGuid() });
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
item.Title = "xxx";
repos.Update(item);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
Console.WriteLine(repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ToSql());
repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ExecuteAffrows();
item = repos.Find(item.Id);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
}
[Fact]
public void UpdateAttach() {
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
var item = new AddUpdateInfo { Id = Guid.NewGuid() };
repos.Attach(item);
item.Title = "xxx";
repos.Update(item);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
Console.WriteLine(repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ToSql());
repos.UpdateDiy.Where(a => a.Id == item.Id).Set(a => a.Clicks + 1).ExecuteAffrows();
item = repos.Find(item.Id);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
}
[Fact]
public void UpdateWhenNotExists() {
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
var item = new AddUpdateInfo { Id = Guid.NewGuid() };
item.Title = "xxx";
Assert.Throws<Exception>(() => repos.Update(item));
}
[Fact]
public void Update() {
g.sqlite.Insert(new AddUpdateInfo()).ExecuteAffrows();
var repos = g.sqlite.GetGuidRepository<AddUpdateInfo>();
var item = new AddUpdateInfo { Id = g.sqlite.Select<AddUpdateInfo>().First().Id };
item.Title = "xxx";
repos.Update(item);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
}
public class AddUpdateInfo {
public Guid Id { get; set; }
public string Title { get; set; }
public int Clicks { get; set; } = 10;
}
[Fact]
public void UnitOfWorkRepository() {
foreach (var fsql in new[] { g.sqlite, /*g.mysql, g.pgsql, g.oracle, g.sqlserver*/ }) {
fsql.CodeFirst.ConfigEntity<FlowModel>(f => {
f.Property(b => b.UserId).IsPrimary(true);
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
f.Property(b => b.Name).IsNullable(false);
});
FlowModel flow = new FlowModel() {
CreateTime = DateTime.Now,
Name = "aaa",
LastModifyTime = DateTime.Now,
UserId = 1,
};
var flowRepos = fsql.GetRepository<FlowModel>();
flowRepos.Insert(flow);
//事务添加
flow = new FlowModel() {
CreateTime = DateTime.Now,
Name = "aaa",
LastModifyTime = DateTime.Now,
UserId = 1,
};
using (var uow = fsql.CreateUnitOfWork()) {
flowRepos = uow.GetRepository<FlowModel>();
flowRepos.Insert(flow);
uow.Commit();
}
}
}
[Fact]
public void UnitOfWorkRepositoryWithDisableBeforeInsert()
{
foreach (var fsql in new[] { g.sqlite, })
{
fsql.CodeFirst.ConfigEntity<FlowModel>(f => {
f.Property(b => b.UserId).IsPrimary(true);
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
f.Property(b => b.Name).IsNullable(false);
});
var flowRepos = fsql.GetRepository<FlowModel>();
var flow = new FlowModel()
{
CreateTime = DateTime.Now,
Name = "aaa",
LastModifyTime = DateTime.Now,
UserId = 1,
};
//清理掉数据库中已存在的数据,为了接下来的插入测试
flowRepos.Delete(a => a.UserId == 1 &&a.Name== "aaa");
using (var uow = fsql.CreateUnitOfWork())
{
//关闭工作单元(不会开始事务)
uow.Close();
var uowFlowRepos = uow.GetRepository<FlowModel>();
uowFlowRepos.Insert(flow);
//已关闭工作单元提不提交都没影响此处注释来确定工作单元开关是否生效关闭了不Commit也应该插入数据
//uow.Commit();
}
Assert.True(flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"));
}
}
[Fact]
public void UnitOfWorkRepositoryWithDisableAfterInsert()
{
foreach (var fsql in new[] {g.sqlite,})
{
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
{
f.Property(b => b.UserId).IsPrimary(true);
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
f.Property(b => b.Name).IsNullable(false);
});
var flowRepos = fsql.GetRepository<FlowModel>();
//清理掉数据库中已存在的数据,为了接下来的插入测试
flowRepos.Delete(a => a.UserId == 1 && a.Name == "aaa");
var flow = new FlowModel()
{
CreateTime = DateTime.Now,
Name = "aaa",
LastModifyTime = DateTime.Now,
UserId = 1,
};
Assert.Throws<Exception>(() =>
{
using (var uow = fsql.CreateUnitOfWork())
{
var uowFlowRepos = uow.GetRepository<FlowModel>();
uowFlowRepos.Insert(flow);
//有了任意 Insert/Update/Delete 调用关闭uow的方法将会发生异常
uow.Close();
uow.Commit();
}
});
}
}
[Fact]
public void UnitOfWorkRepositoryWithoutDisable()
{
foreach (var fsql in new[] { g.sqlite, })
{
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
{
f.Property(b => b.UserId).IsPrimary(true);
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
f.Property(b => b.Name).IsNullable(false);
});
var flowRepos = fsql.GetRepository<FlowModel>();
if (flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"))
{
flowRepos.Delete(a => a.UserId == 1);
}
var flow = new FlowModel()
{
CreateTime = DateTime.Now,
Name = "aaa",
LastModifyTime = DateTime.Now,
UserId = 1,
};
using (var uow = fsql.CreateUnitOfWork())
{
var uowFlowRepos = uow.GetRepository<FlowModel>();
uowFlowRepos.Insert(flow);
//不调用commit将不会提交数据库更改
//uow.Commit();
}
Assert.False(flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"));
}
}
public partial class FlowModel {
public int UserId { get; set; }
public int Id { get; set; }
public int? ParentId { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
public DateTime LastModifyTime { get; set; }
public string Desc { get; set; }
}
[Fact]
public void AsType() {
g.sqlite.Insert(new AddUpdateInfo()).ExecuteAffrows();
var repos = g.sqlite.GetGuidRepository<object>();
repos.AsType(typeof(AddUpdateInfo));
var item = new AddUpdateInfo();
repos.Insert(item);
repos.Update(item);
item.Clicks += 1;
repos.InsertOrUpdate(item);
var item2 = repos.Find(item.Id) as AddUpdateInfo;
Assert.Equal(item.Clicks, item2.Clicks);
repos.DataFilter.Apply("xxx", a => (a as AddUpdateInfo).Clicks == 2);
Assert.Null(repos.Find(item.Id));
}
}
}

View File

@ -0,0 +1,160 @@
using FreeSql.DataAnnotations;
using FreeSql;
using System;
using System.Collections.Generic;
using Xunit;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace FreeSql.Tests {
public class UnitTest1 {
class testenumWhere {
public Guid id { get; set; }
public testenumWhereType type { get; set; }
}
public enum testenumWhereType { Menu, Class, Blaaa }
[Fact]
public void Include_ManyToMany() {
g.sqlite.CodeFirst.SyncStructure<Song_tag>();
g.sqlite.CodeFirst.SyncStructure<Tag>();
g.sqlite.CodeFirst.SyncStructure<Song>();
using (var ctx = g.sqlite.CreateDbContext()) {
var songs = ctx.Set<Song>().Select
.IncludeMany(a => a.Tags)
.ToList();
var tag1 = new Tag {
Ddd = DateTime.Now.Second,
Name = "test_manytoMany_01_中国"
};
var tag2 = new Tag {
Ddd = DateTime.Now.Second,
Name = "test_manytoMany_02_美国"
};
var tag3 = new Tag {
Ddd = DateTime.Now.Second,
Name = "test_manytoMany_03_日本"
};
ctx.AddRange(new[] { tag1, tag2, tag3 });
var song1 = new Song {
Create_time = DateTime.Now,
Title = "test_manytoMany_01_我是中国人.mp3",
Url = "http://ww.baidu.com/"
};
var song2 = new Song {
Create_time = DateTime.Now,
Title = "test_manytoMany_02_爱你一万年.mp3",
Url = "http://ww.163.com/"
};
var song3 = new Song {
Create_time = DateTime.Now,
Title = "test_manytoMany_03_千年等一回.mp3",
Url = "http://ww.sina.com/"
};
ctx.AddRange(new[] { song1, song2, song3 });
ctx.AddRange(
new[] {
new Song_tag { Song_id = song1.Id, Tag_id = tag1.Id },
new Song_tag { Song_id = song2.Id, Tag_id = tag1.Id },
new Song_tag { Song_id = song3.Id, Tag_id = tag1.Id },
new Song_tag { Song_id = song1.Id, Tag_id = tag2.Id },
new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id },
new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id },
}
);
ctx.SaveChanges();
}
}
[Fact]
public void Add() {
g.sqlite.SetDbContextOptions(opt => {
//opt.EnableAddOrUpdateNavigateList = false;
});
g.mysql.Insert<testenumWhere>().AppendData(new testenumWhere { type = testenumWhereType.Blaaa }).ExecuteAffrows();
var sql = g.mysql.Select<testenumWhere>().Where(a => a.type == testenumWhereType.Blaaa).ToSql();
var tolist = g.mysql.Select<testenumWhere>().Where(a => a.type == testenumWhereType.Blaaa).ToList();
//支持 1对多 联级保存
using (var ctx = new FreeContext(g.sqlite)) {
var tags = ctx.Set<Tag>().Select.IncludeMany(a => a.Tags).ToList();
var tag = new Tag {
Name = "testaddsublist",
Tags = new[] {
new Tag { Name = "sub1" },
new Tag { Name = "sub2" },
new Tag {
Name = "sub3",
Tags = new[] {
new Tag { Name = "sub3_01" }
}
}
}
};
ctx.Add(tag);
ctx.SaveChanges();
}
}
[Fact]
public void Update() {
//查询 1对多再联级保存
using (var ctx = new FreeContext(g.sqlite)) {
var tag = ctx.Set<Tag>().Select.First();
tag.Tags.Add(new Tag { Name = "sub3" });
ctx.Update(tag);
ctx.SaveChanges();
}
}
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; }
[Column(IsVersion = true)]
public long versionRow { 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; }
}
}
}

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
public class g {
static Lazy<IFreeSql> sqlserverLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后
.UseLazyLoading(true)
.UseNoneCommandParameter(true)
.Build());
public static IFreeSql sqlserver => sqlserverLazy.Value;
static Lazy<IFreeSql> mysqlLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
.UseAutoSyncStructure(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后
.UseLazyLoading(true)
.UseNoneCommandParameter(true)
.Build());
public static IFreeSql mysql => mysqlLazy.Value;
static Lazy<IFreeSql> pgsqlLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=10")
.UseAutoSyncStructure(true)
.UseSyncStructureToLower(true)
.UseLazyLoading(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后
.UseNoneCommandParameter(true)
.Build());
public static IFreeSql pgsql => pgsqlLazy.Value;
static Lazy<IFreeSql> oracleLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseSyncStructureToUpper(true)
.UseNoneCommandParameter(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后
.Build());
public static IFreeSql oracle => oracleLazy.Value;
static Lazy<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|/document22.db;Attachs=xxxtb.db;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后
.UseNoneCommandParameter(true)
.Build());
public static IFreeSql sqlite => sqliteLazy.Value;
}

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.MySql\FreeSql.Provider.MySql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Oracle\FreeSql.Provider.Oracle.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.PostgreSQL\FreeSql.Provider.PostgreSQL.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.SqlServer\FreeSql.Provider.SqlServer.csproj" />
</ItemGroup>
</Project>

View File

@ -13,9 +13,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" /> <ProjectReference Include="..\..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
<ProjectReference Include="..\FreeSql\FreeSql.csproj" /> <ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.MySqlConnector\FreeSql.Provider.MySqlConnector.csproj" /> <ProjectReference Include="..\..\Providers\FreeSql.Provider.MySqlConnector\FreeSql.Provider.MySqlConnector.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -11,23 +11,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FreeSql.DbContext" Version="0.6.4.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.8" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" /> <PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.MySql\FreeSql.Provider.MySql.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.Oracle\FreeSql.Provider.Oracle.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.PostgreSQL\FreeSql.Provider.PostgreSQL.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
<ProjectReference Include="..\Providers\FreeSql.Provider.SqlServer\FreeSql.Provider.SqlServer.csproj" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="DataAnnotations\MySql\" /> <Folder Include="DataAnnotations\MySql\" />
<Folder Include="DataAnnotations\SqlServer\" /> <Folder Include="DataAnnotations\SqlServer\" />
@ -35,4 +24,15 @@
<Folder Include="Other\" /> <Folder Include="Other\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.LazyLoading\FreeSql.Extensions.LazyLoading.csproj" />
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.MySql\FreeSql.Provider.MySql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Oracle\FreeSql.Provider.Oracle.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.PostgreSQL\FreeSql.Provider.PostgreSQL.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.SqlServer\FreeSql.Provider.SqlServer.csproj" />
</ItemGroup>
</Project> </Project>

Some files were not shown because too many files have changed in this diff Show More