mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
Include 贪婪加载第一版,已通过集合的导航数据加载,包括 OneToMany/ManyToMany
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FreeSql.DbContext" Version="0.5.8" />
|
||||
<PackageReference Include="FreeSql.DbContext" Version="0.5.12.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.8" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
|
@ -756,5 +756,64 @@ namespace FreeSql.Tests.Sqlite {
|
||||
sql = query.ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22AsTable1\" a LEFT JOIN \"TestTypeInfo\" b on b.\"Guid\" = a.\"TypeGuid\" and b.\"Name\" = @bname", sql);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Include_OneToMany() {
|
||||
|
||||
}
|
||||
[Fact]
|
||||
public void Include_OneToChilds() {
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Include_ManyToMany() {
|
||||
|
||||
var tag1 = new Tag {
|
||||
Ddd = DateTime.Now.Second,
|
||||
Name = "test_manytoMany_01_中国"
|
||||
};
|
||||
tag1.Id = (int)g.sqlite.Insert(tag1).ExecuteIdentity();
|
||||
var tag2 = new Tag {
|
||||
Ddd = DateTime.Now.Second,
|
||||
Name = "test_manytoMany_02_美国"
|
||||
};
|
||||
tag2.Id = (int)g.sqlite.Insert(tag2).ExecuteIdentity();
|
||||
var tag3 = new Tag {
|
||||
Ddd = DateTime.Now.Second,
|
||||
Name = "test_manytoMany_03_日本"
|
||||
};
|
||||
tag3.Id = (int)g.sqlite.Insert(tag3).ExecuteIdentity();
|
||||
|
||||
var song1 = new Song {
|
||||
Create_time = DateTime.Now,
|
||||
Title = "test_manytoMany_01_我是中国人.mp3",
|
||||
Url = "http://ww.baidu.com/"
|
||||
};
|
||||
song1.Id = (int)g.sqlite.Insert(song1).ExecuteIdentity();
|
||||
var song2 = new Song {
|
||||
Create_time = DateTime.Now,
|
||||
Title = "test_manytoMany_02_爱你一万年.mp3",
|
||||
Url = "http://ww.163.com/"
|
||||
};
|
||||
song2.Id = (int)g.sqlite.Insert(song2).ExecuteIdentity();
|
||||
var song3 = new Song {
|
||||
Create_time = DateTime.Now,
|
||||
Title = "test_manytoMany_03_千年等一回.mp3",
|
||||
Url = "http://ww.sina.com/"
|
||||
};
|
||||
song3.Id = (int)g.sqlite.Insert(song3).ExecuteIdentity();
|
||||
|
||||
g.sqlite.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag1.Id }).ExecuteAffrows();
|
||||
g.sqlite.Insert(new Song_tag { Song_id = song2.Id, Tag_id = tag1.Id }).ExecuteAffrows();
|
||||
g.sqlite.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag1.Id }).ExecuteAffrows();
|
||||
g.sqlite.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag2.Id }).ExecuteAffrows();
|
||||
g.sqlite.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id }).ExecuteAffrows();
|
||||
g.sqlite.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id }).ExecuteAffrows();
|
||||
|
||||
var songs = g.sqlite.Select<Song>()
|
||||
.IncludeMany(a => a.Tags)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -94,6 +94,8 @@ namespace FreeSql.Tests {
|
||||
|
||||
public ICollection<Model2> Childs { get; set; }
|
||||
|
||||
public int M2Id { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class Model2 {
|
||||
@ -110,6 +112,32 @@ namespace FreeSql.Tests {
|
||||
[Fact]
|
||||
public void Test1() {
|
||||
|
||||
g.sqlite.GetRepository<Model1, int>().Insert(new Model1 {
|
||||
title = "test_" + DateTime.Now.ToString("yyyyMMddHHmmss"),
|
||||
M2Id = DateTime.Now.Second + DateTime.Now.Minute,
|
||||
Childs = new[] {
|
||||
new Model2 {
|
||||
title = "model2Test_title_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "0001",
|
||||
},
|
||||
new Model2 {
|
||||
title = "model2Test_title_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "0002",
|
||||
},
|
||||
new Model2 {
|
||||
title = "model2Test_title_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "0003",
|
||||
},
|
||||
new Model2 {
|
||||
title = "model2Test_title_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "0004",
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var includet1 = g.sqlite.Select<Model1>()
|
||||
.IncludeMany(a => a.Childs, s => s.Where(a => a.id > 0))
|
||||
.Where(a => a.id > 10)
|
||||
.ToList();
|
||||
|
||||
|
||||
|
||||
var ttt1 = g.sqlite.Select<Model1>().Where(a => a.Childs.AsSelect().Any(b => b.title == "111")).ToList();
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user