mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-07-03 18:58:14 +08:00
initial commit
This commit is contained in:
@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<WarningLevel>3</WarningLevel>
|
||||
<NoWarn>1701;1702;1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.BaseEntity\FreeSql.Extensions.BaseEntity.csproj" />
|
||||
<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>
|
1213
FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs
Normal file
1213
FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs
Normal file
File diff suppressed because it is too large
Load Diff
153
FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests02.cs
Normal file
153
FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests02.cs
Normal file
@ -0,0 +1,153 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace FreeSql.Tests.DbContext
|
||||
{
|
||||
public class RepositoryTests02
|
||||
{
|
||||
[Fact]
|
||||
public void TestMethod1()
|
||||
{
|
||||
using (IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=:memory:")
|
||||
.UseMonitorCommand(cmd => Trace.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句
|
||||
.UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
|
||||
.Build())
|
||||
{
|
||||
fsql.GlobalFilter.ApplyIf<User>("TenantFilter", () => TenantManager.Current > 0, a => a.TenantId == TenantManager.Current);
|
||||
|
||||
fsql.Aop.AuditValue += (_, e) =>
|
||||
{
|
||||
if (TenantManager.Current > 0 && e.Property.PropertyType == typeof(int) && e.Property.Name == "TenantId")
|
||||
{
|
||||
e.Value = TenantManager.Current;
|
||||
};
|
||||
};
|
||||
|
||||
IBaseRepository<User> resp = fsql.GetRepository<User>();
|
||||
resp.Delete(a => a.ID != null);
|
||||
Assert.True(resp != null);
|
||||
|
||||
|
||||
|
||||
TenantManager.Current = 1;
|
||||
|
||||
resp.InsertOrUpdate(new User()
|
||||
{
|
||||
uname = "zhaoqin",
|
||||
});
|
||||
|
||||
resp.InsertOrUpdate(new User()
|
||||
{
|
||||
uname = "wanghuan",
|
||||
});
|
||||
long cc = resp.Where(a => a.ID != null).Count();
|
||||
Assert.True(cc == 2);
|
||||
|
||||
|
||||
|
||||
TenantManager.Current = 2;
|
||||
|
||||
resp.InsertOrUpdate(new User()
|
||||
{
|
||||
uname = "zhaoqin1",
|
||||
});
|
||||
|
||||
resp.InsertOrUpdate(new User()
|
||||
{
|
||||
uname = "wanghuan1",
|
||||
});
|
||||
long c = resp.Where(a => a.ID != null).Count();
|
||||
Assert.True(c == 2);
|
||||
|
||||
|
||||
|
||||
TenantManager.Current = 0;
|
||||
|
||||
Assert.True(resp.Where(a => a.ID != null).Count() == 4);
|
||||
|
||||
|
||||
//多租户启用,但表达式想取消,这个可以成功
|
||||
TenantManager.Current = 2;
|
||||
long count1 = fsql.Select<User>().DisableGlobalFilter().Count();
|
||||
Assert.True(count1 == 4);
|
||||
|
||||
|
||||
Console.WriteLine("仓储的过滤器禁止,但不成功.");
|
||||
//仓储的过滤器禁止,但不成功.
|
||||
using (resp.DataFilter.DisableAll())
|
||||
{
|
||||
|
||||
long count2 = resp.Where(a => a.ID != null).Count();
|
||||
|
||||
Assert.True(count2 == 4);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class TenantManager
|
||||
{
|
||||
// 注意一定是 static 静态化
|
||||
static AsyncLocal<int> _asyncLocal = new AsyncLocal<int>();
|
||||
|
||||
public static int Current
|
||||
{
|
||||
get => _asyncLocal.Value;
|
||||
set => _asyncLocal.Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public class BaseModel
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int? ID { get; set; }
|
||||
public int TenantId { get; set; }
|
||||
}
|
||||
|
||||
public class User : BaseModel
|
||||
{
|
||||
|
||||
public Guid cateId { get; set; }
|
||||
public Cate cate { get; set; }
|
||||
|
||||
public string uname { get; set; }
|
||||
public int age { get; set; }
|
||||
|
||||
public List<Group> groups { get; set; } = new List<Group>();
|
||||
}
|
||||
|
||||
public class Cate : BaseModel
|
||||
{
|
||||
public string catename { get; set; }
|
||||
|
||||
public List<User> users { get; set; }
|
||||
}
|
||||
|
||||
public class Group : BaseModel
|
||||
{
|
||||
public string groupname { get; set; }
|
||||
|
||||
public List<User> users { get; set; } = new List<User>();
|
||||
}
|
||||
|
||||
public class User_Group
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
public User user { get; set; }
|
||||
|
||||
public Guid GroupId { get; set; }
|
||||
public Group group { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
285
FreeSql.Tests/FreeSql.Tests.DbContext/USERINFO.cs
Normal file
285
FreeSql.Tests/FreeSql.Tests.DbContext/USERINFO.cs
Normal file
@ -0,0 +1,285 @@
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// 员工信息表
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[Index("员工代码badgenumber唯一", "badgenumber", true)]
|
||||
public class userinfo : BaseEntity<userinfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// 员工ID
|
||||
/// </summary>
|
||||
[Column(IsPrimary = true)]
|
||||
[System.ComponentModel.DisplayName("员工ID ")]
|
||||
[System.ComponentModel.DataAnnotations.Required()]
|
||||
public int userid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 工号
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("工号")]
|
||||
[System.ComponentModel.DataAnnotations.Required()]
|
||||
[Column(Name = "BADGENUMBER", DbType = "VARCHAR(24)")]
|
||||
public String badgenumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 姓名
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("姓名")]
|
||||
[System.ComponentModel.DataAnnotations.Required()]
|
||||
[Column(DbType = "varchar(40) NULL")]
|
||||
|
||||
public String Name { get; set; }
|
||||
/// <summary>
|
||||
/// 身份证
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("身份证证")]
|
||||
[System.ComponentModel.DataAnnotations.Required()]
|
||||
public String IDCardNo { get; set; }
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 行动电话
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("行动电话")]
|
||||
[Column(DbType = "varchar(20) NULL")]
|
||||
public string pager { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 邮件地址
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("邮件地址 ")]
|
||||
public String email { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 办公电话
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("办公电话")]
|
||||
[Column(DbType = "varchar(20) NULL")]
|
||||
public String ophone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 入职时间
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("入职时间")]
|
||||
[Column(DbType = "date")]
|
||||
public DateTime? hiredday { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 生日
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("生日 ")]
|
||||
[Column(DbType = "date")]
|
||||
public DateTime? birthday { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 民族
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("民族")]
|
||||
public string minzu { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 籍贯
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("籍贯")]
|
||||
public String homeaddress { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 合同日期
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("合同日期")]
|
||||
[Column(DbType = "date")]
|
||||
public DateTime? hetongdate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 家庭地址
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("家庭地址")]
|
||||
[Column(DbType = "varchar(80) NULL")]
|
||||
public String street { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 邮编
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("邮编")]
|
||||
[Column(DbType = "varchar(12) NULL")]
|
||||
public String zip { get; set; }
|
||||
|
||||
[System.ComponentModel.DisplayName("城市")]
|
||||
[Column(Name = "CITY", DbType = "varchar(2)")]
|
||||
public string CITY { get; set; }
|
||||
|
||||
[System.ComponentModel.DisplayName("省份")]
|
||||
[Column(DbType = "varchar(2) NULL")]
|
||||
public string STATE { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 编号
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("编号")]
|
||||
|
||||
public string ssn { get; set; }
|
||||
|
||||
[Column(DbType = "varchar(8) NULL")]
|
||||
public string GENDER { get; set; } = "M";
|
||||
/// <summary>
|
||||
/// 职务
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("职务")]
|
||||
[Column(DbType = "varchar(20) NULL")]
|
||||
public string title { get; set; }
|
||||
|
||||
|
||||
public short? VERIFICATIONMETHOD { get; set; }//验证方式
|
||||
public short? DEFAULTDEPTID { get; set; } = 1;//所属部门ID号
|
||||
public short? ATT { get; set; } = 1;//考勤有效
|
||||
public short? INLATE { get; set; } = 1;//计迟到
|
||||
public short? OUTEARLY { get; set; } = 1;//计早退
|
||||
|
||||
public short? OVERTIME { get; set; }
|
||||
|
||||
public short? SEP { get; set; } = 1;
|
||||
public short HOLIDAY { get; set; } = 1;//假日休息
|
||||
public string PASSWORD { get; set; }//口令
|
||||
public short LUNCHDURATION { get; set; } = 1;//有午休
|
||||
public string MVerifyPass { get; set; }//考勤验证密码
|
||||
|
||||
//[Column(DbType = "image NULL")]
|
||||
//public byte[] PHOTO { get; set; }
|
||||
//[Column(DbType = "image NULL")]
|
||||
//public byte[] Notes { get; set; }
|
||||
|
||||
public int? VerifyCode { get; set; }
|
||||
public int? Expires { get; set; }
|
||||
public int? ValidCount { get; set; }
|
||||
public int? UseAccGroupTZ { get; set; }
|
||||
|
||||
public int? AccGroup { get; set; }
|
||||
public int? FaceGroup { get; set; }
|
||||
public int? EMPRIVILEGE { get; set; }
|
||||
public int? InheritDeptRule { get; set; }
|
||||
public int? RegisterOT { get; set; }
|
||||
public int? MinAutoSchInterval { get; set; }
|
||||
public int? AutoSchPlan { get; set; }
|
||||
public int? InheritDeptSchClass { get; set; }
|
||||
public int? InheritDeptSch { get; set; }
|
||||
public int? privilege { get; set; }
|
||||
public int? TimeZone1 { get; set; }
|
||||
public int? TimeZone2 { get; set; }
|
||||
public int? TimeZone3 { get; set; }
|
||||
|
||||
[Column(DbType = "date")]
|
||||
public DateTime? ValidTimeEnd { get; set; }
|
||||
[Column(DbType = "date")]
|
||||
public DateTime? ValidTimeBegin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 家庭电话
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("家庭电话")]
|
||||
[Column(DbType = "varchar(20) NULL")]
|
||||
public String fphone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 卡号
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("卡号 ")]
|
||||
[Column(Name = "CardNo", DbType = "varchar(20)")]
|
||||
public String CardNo { get; set; }
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 身份证有效期
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("身份证有效期 ")]
|
||||
public String idcardvalidtime { get; set; } = new DateTime(2099, 12, 31).ToString();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 离职日期
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("离职日期")]
|
||||
[Column(DbType = "date")]
|
||||
public DateTime? leavedate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 登录密码
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("登录密码")]
|
||||
public String loginpass { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 相片地址
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("相片地址")]
|
||||
public String picurl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上级主管
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("上级主管")]
|
||||
public int? managerid { get; set; }
|
||||
///// <summary>
|
||||
///// 上级主管对象
|
||||
///// </summary>
|
||||
//[Navigate("managerid")]
|
||||
//public userinfo pManager { get; set; }
|
||||
|
||||
|
||||
|
||||
[Navigate(ManyToMany = typeof(dept_user))]
|
||||
public List<DEPARTMENTS> depts { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 管理员标志
|
||||
/// </summary>
|
||||
|
||||
[System.ComponentModel.DisplayName("管理员标志")]
|
||||
public short? SECURITYFLAGS { get; set; }//管理员标志
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
250
FreeSql.Tests/FreeSql.Tests.DbContext/UnitTest1.cs
Normal file
250
FreeSql.Tests/FreeSql.Tests.DbContext/UnitTest1.cs
Normal file
@ -0,0 +1,250 @@
|
||||
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.Delete<userinfo>().Where("1=1").ExecuteAffrows();
|
||||
g.sqlite.Delete<DEPARTMENTS>().Where("1=1").ExecuteAffrows();
|
||||
g.sqlite.Delete<dept_user>().Where("1=1").ExecuteAffrows();
|
||||
BaseEntity.Initialization(g.sqlite, null);
|
||||
|
||||
userinfo user = new userinfo { userid = 1, badgenumber = "", Name="", IDCardNo="" };
|
||||
user.Insert();
|
||||
|
||||
user.depts = new List<DEPARTMENTS>(
|
||||
new[] {
|
||||
new DEPARTMENTS { deptid = 1, deptcode = "01", deptname = "" },
|
||||
new DEPARTMENTS { deptid = 2, deptcode = "02", deptname = "" },
|
||||
new DEPARTMENTS { deptid = 3, deptcode = "03" , deptname = ""},
|
||||
});
|
||||
user.SaveMany("depts");
|
||||
|
||||
user.depts = new List<DEPARTMENTS>(
|
||||
new[] {
|
||||
new DEPARTMENTS { deptid = 1, deptcode = "01", deptname = "" },
|
||||
new DEPARTMENTS { deptid = 2, deptcode = "02", deptname = "" },
|
||||
new DEPARTMENTS { deptid = 4, deptcode = "04", deptname = "" },
|
||||
});
|
||||
user.SaveMany("depts");
|
||||
|
||||
user.depts = new List<DEPARTMENTS>(
|
||||
new[] {
|
||||
new DEPARTMENTS { deptid = 2, deptcode = "02", deptname = "" },
|
||||
});
|
||||
user.SaveMany("depts");
|
||||
|
||||
g.sqlite.CodeFirst.SyncStructure<Song_tag>();
|
||||
g.sqlite.CodeFirst.SyncStructure<Tag>();
|
||||
g.sqlite.CodeFirst.SyncStructure<Song>();
|
||||
|
||||
var test150_01 = g.sqlite.GetRepository<Tag>()
|
||||
.Select.From<Tag>((s, b) => s.InnerJoin(a => a.Id == b.Id))
|
||||
.ToList((a, b) => new
|
||||
{
|
||||
a.Id,
|
||||
a.Name,
|
||||
id2 = b.Id,
|
||||
name2 = b.Name
|
||||
});
|
||||
|
||||
|
||||
using (var ctx = g.sqlite.CreateDbContext())
|
||||
{
|
||||
var setTag = ctx.Set<Tag>();
|
||||
var tags = setTag.Select.Limit(10).ToList();
|
||||
setTag.BeginEdit(tags);
|
||||
|
||||
tags.Add(new Tag
|
||||
{
|
||||
Ddd = DateTime.Now.Second,
|
||||
Name = "test_manytoMany_01_中国2234234"
|
||||
});
|
||||
tags[0].Name = "123123";
|
||||
tags.RemoveAt(1);
|
||||
|
||||
//tags.Clear();
|
||||
|
||||
Assert.Equal(3, setTag.EndEdit());
|
||||
|
||||
var test150_02 = ctx.Set<Tag>()
|
||||
.Select.From<Tag>((s, b) => s.InnerJoin(a => a.Id == b.Id))
|
||||
.ToList((a, b) => new
|
||||
{
|
||||
a.Id,a.Name,
|
||||
id2 = b.Id, name2 = b.Name
|
||||
});
|
||||
|
||||
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.Orm.Select<Tag>().Limit(10).ToList();
|
||||
|
||||
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.EnableCascadeSave = 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 = g.sqlite.CreateDbContext())
|
||||
{
|
||||
ctx.Options.EnableCascadeSave = true;
|
||||
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);
|
||||
|
||||
var tags2 = ctx.Orm.Select<Tag>().IncludeMany(a => a.Tags).ToList();
|
||||
|
||||
ctx.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update()
|
||||
{
|
||||
//查询 1对多,再级联保存
|
||||
|
||||
using (var ctx = g.sqlite.CreateDbContext())
|
||||
{
|
||||
ctx.Options.EnableCascadeSave = true;
|
||||
var tag = ctx.Set<Tag>().Select.First();
|
||||
tag.Tags.Add(new Tag { Name = "sub3" });
|
||||
tag.Name = Guid.NewGuid().ToString();
|
||||
ctx.Update(tag);
|
||||
var xxx = ctx.Orm.Select<Tag>().First();
|
||||
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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
96
FreeSql.Tests/FreeSql.Tests.DbContext/departments(1).cs
Normal file
96
FreeSql.Tests/FreeSql.Tests.DbContext/departments(1).cs
Normal file
@ -0,0 +1,96 @@
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 部门表
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[Index("部门代码deptcode唯一", "deptcode", true)]
|
||||
public class DEPARTMENTS : BaseEntity<DEPARTMENTS>
|
||||
{
|
||||
/// <summary>
|
||||
/// 部门ID
|
||||
/// </summary>
|
||||
[Column(IsPrimary = true)]
|
||||
|
||||
[System.ComponentModel.DisplayName("部门ID")]
|
||||
public int deptid { get; set; }
|
||||
|
||||
///// <summary>
|
||||
///// 员工列表 对应employee.deptid
|
||||
///// </summary>
|
||||
//[Navigate("deptid")]
|
||||
//public List<employee> Employees { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上级部门ID
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("上级部门ID")]
|
||||
public int? supdeptid { get; set; }
|
||||
/// <summary>
|
||||
/// 上级部门对象
|
||||
/// </summary>
|
||||
[Navigate("supdeptid")]
|
||||
public DEPARTMENTS pDepartments { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 部门主管ID
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("部门主管ID")]
|
||||
public int? managerid { get; set; }
|
||||
/// <summary>
|
||||
/// 部门主管对象
|
||||
/// </summary>
|
||||
[Navigate("managerid")]
|
||||
public userinfo manager { get; set; }
|
||||
|
||||
|
||||
///// <summary>
|
||||
///// 下级部门列表
|
||||
///// </summary>
|
||||
//[Navigate("supdeptid")]
|
||||
//public List<departments> childDepartments { get; set; }
|
||||
|
||||
|
||||
[Navigate(ManyToMany = typeof(dept_user))]
|
||||
public List<userinfo> employeesMany { get; set; }
|
||||
|
||||
|
||||
#region MyRegion
|
||||
/// <summary>
|
||||
/// 部门代码
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("部门代码")]
|
||||
[System.ComponentModel.DataAnnotations.Required()]
|
||||
public string deptcode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 部门名称
|
||||
/// </summary>
|
||||
[System.ComponentModel.DisplayName("部门名称")]
|
||||
[System.ComponentModel.DataAnnotations.Required()]
|
||||
public string deptname { get; set; }
|
||||
#endregion
|
||||
|
||||
public short? InheritParentSch { get; set; }
|
||||
public short? InheritDeptSch { get; set; }
|
||||
public short? InheritDeptSchClass { get; set; }
|
||||
public short? AutoSchPlan { get; set; }
|
||||
public short? InLate { get; set; }
|
||||
public short? OutEarly { get; set; }
|
||||
public short? InheritDeptRule { get; set; }
|
||||
public int? MinAutoSchInterval { get; set; }
|
||||
public short? RegisterOT { get; set; }
|
||||
public int? DefaultSchId { get; set; }
|
||||
public short? ATT { get; set; }
|
||||
public short? Holiday { get; set; }
|
||||
public short? OverTime { get; set; }
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
21
FreeSql.Tests/FreeSql.Tests.DbContext/dept_user.cs
Normal file
21
FreeSql.Tests/FreeSql.Tests.DbContext/dept_user.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FreeSql;
|
||||
|
||||
public class dept_user: BaseEntity<dept_user>
|
||||
{
|
||||
public int deptid { get; set; }
|
||||
public int userid { get; set; }
|
||||
|
||||
[Navigate("deptid")]
|
||||
public DEPARTMENTS dept { get; set; }
|
||||
|
||||
|
||||
[Navigate("userid")]
|
||||
public userinfo emp { get; set; }
|
||||
}
|
||||
|
97
FreeSql.Tests/FreeSql.Tests.DbContext/g.cs
Normal file
97
FreeSql.Tests/FreeSql.Tests.DbContext/g.cs
Normal file
@ -0,0 +1,97 @@
|
||||
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;TrustServerCertificate=true")
|
||||
.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=127.0.0.1;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=10")
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseNameConvert(FreeSql.Internal.NameConvertType.ToLower)
|
||||
.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)
|
||||
.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
|
||||
.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;
|
||||
}
|
Reference in New Issue
Block a user