- 合并 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,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);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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,
};
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>Ѵ<EFBFBD><D1B4>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ݣ<EFBFBD>Ϊ<EFBFBD>˽<EFBFBD><CBBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
flowRepos.Delete(a => a.UserId == 1 &&a.Name== "aaa");
using (var uow = fsql.CreateUnitOfWork())
{
//<2F>رչ<D8B1><D5B9><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ʼ<E1BFAA><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uow.Close();
var uowFlowRepos = uow.GetRepository<FlowModel>();
uowFlowRepos.Insert(flow);
//<2F>ѹرչ<D8B1><D5B9><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><E1BDBB>ûӰ<C3BB><EFBFBD>˴<EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD>ر<EFBFBD><D8B1>ˣ<EFBFBD><CBA3><EFBFBD>CommitҲӦ<D2B2>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//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>();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>Ѵ<EFBFBD><D1B4>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ݣ<EFBFBD>Ϊ<EFBFBD>˽<EFBFBD><CBBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Insert/Update/Delete <20><><EFBFBD>ùر<C3B9>uow<6F>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B7A2><EFBFBD>
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);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>commit<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1BDBB><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
//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_<31>й<EFBFBD>"
};
var tag2 = new Tag {
Ddd = DateTime.Now.Second,
Name = "test_manytoMany_02_<32><5F><EFBFBD><EFBFBD>"
};
var tag3 = new Tag {
Ddd = DateTime.Now.Second,
Name = "test_manytoMany_03_<33>ձ<EFBFBD>"
};
ctx.AddRange(new[] { tag1, tag2, tag3 });
var song1 = new Song {
Create_time = DateTime.Now,
Title = "test_manytoMany_01_<31><5F><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD>.mp3",
Url = "http://ww.baidu.com/"
};
var song2 = new Song {
Create_time = DateTime.Now,
Title = "test_manytoMany_02_<32><5F><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>.mp3",
Url = "http://ww.163.com/"
};
var song3 = new Song {
Create_time = DateTime.Now,
Title = "test_manytoMany_03_ǧ<5F><C7A7><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>.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();
//֧<><D6A7> 1<>Զ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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() {
//<2F><>ѯ 1<>Զ࣬<D4B6><E0A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
}