From be0f48b629a814cb0038740a4f5ec7719e9230a8 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Sat, 11 May 2019 17:28:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=B6=E4=BB=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=EF=BC=8CInclude=20=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataContext/SqlServer/SqlServerFixture.cs | 2 +- FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs | 136 +++++++++++++++++ FreeSql.Tests/Oracle/Curd/OracleSelectTest.cs | 136 +++++++++++++++++ .../PostgreSQL/Curd/PostgreSQLSelectTest.cs | 136 +++++++++++++++++ .../SqlServer/Curd/SqlServerSelectTest.cs | 140 +++++++++++++++++- FreeSql.Tests/UnitTest1.cs | 4 +- FreeSql.Tests/g.cs | 10 +- 7 files changed, 557 insertions(+), 7 deletions(-) diff --git a/FreeSql.Tests/DataContext/SqlServer/SqlServerFixture.cs b/FreeSql.Tests/DataContext/SqlServer/SqlServerFixture.cs index 09bbe05e..9561b14c 100644 --- a/FreeSql.Tests/DataContext/SqlServer/SqlServerFixture.cs +++ b/FreeSql.Tests/DataContext/SqlServer/SqlServerFixture.cs @@ -35,7 +35,7 @@ namespace FreeSql.Tests.DataContext.SqlServer SqlServer.Ado.ExecuteNonQuery("DROP SCHEMA dbo2"); } - var tempTables = new string[] { "cccccdddwww", "song", "tb_alltype", "tb_topic", "tb_topic22", + var tempTables = new string[] { "cccccdddwww", "song", "tag", "Song_tag", "tb_alltype", "tb_topic", "tb_topic22", "tb_topic22211", "tb_topic111333", "TestTypeInfo", "TestTypeInfo333", "TestTypeParentInfo", "TestTypeParentInfo23123", "xxdkdkdk1222", "xxx"}; foreach (var tempTable in tempTables) diff --git a/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs b/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs index 218e2d35..32b237ef 100644 --- a/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs +++ b/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs @@ -900,5 +900,141 @@ namespace FreeSql.Tests.MySql { sql = query.ToSql().Replace("\r\n", ""); Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` 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() { + var tag1 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_中国" + }; + tag1.Id = (int)g.mysql.Insert(tag1).ExecuteIdentity(); + var tag1_1 = new Tag { + Parent_id = tag1.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_北京" + }; + tag1_1.Id = (int)g.mysql.Insert(tag1_1).ExecuteIdentity(); + var tag1_2 = new Tag { + Parent_id = tag1.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_上海" + }; + tag1_2.Id = (int)g.mysql.Insert(tag1_2).ExecuteIdentity(); + + var tag2 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_美国" + }; + tag2.Id = (int)g.mysql.Insert(tag2).ExecuteIdentity(); + var tag2_1 = new Tag { + Parent_id = tag2.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_纽约" + }; + tag2_1.Id = (int)g.mysql.Insert(tag2_1).ExecuteIdentity(); + var tag2_2 = new Tag { + Parent_id = tag2.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_华盛顿" + }; + tag2_2.Id = (int)g.mysql.Insert(tag2_2).ExecuteIdentity(); + + var tags0 = g.mysql.Select() + .Include(a => a.Parent) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags = g.mysql.Select() + .IncludeMany(a => a.Tags) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags2 = g.mysql.Select() + .IncludeMany(a => a.Tags, + then => then.Include(a => a.Parent).IncludeMany(a => a.Songs)) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags3 = g.mysql.Select() + .IncludeMany(a => a.Tags, + then => then.Include(a => a.Parent).IncludeMany(a => a.Songs).IncludeMany(a => a.Tags)) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + } + + [Fact] + public void Include_ManyToMany() { + + var tag1 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_01_中国" + }; + tag1.Id = (int)g.mysql.Insert(tag1).ExecuteIdentity(); + var tag2 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_02_美国" + }; + tag2.Id = (int)g.mysql.Insert(tag2).ExecuteIdentity(); + var tag3 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_03_日本" + }; + tag3.Id = (int)g.mysql.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.mysql.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.mysql.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.mysql.Insert(song3).ExecuteIdentity(); + + g.mysql.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.mysql.Insert(new Song_tag { Song_id = song2.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.mysql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.mysql.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag2.Id }).ExecuteAffrows(); + g.mysql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id }).ExecuteAffrows(); + g.mysql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id }).ExecuteAffrows(); + + var songs = g.mysql.Select() + .IncludeMany(a => a.Tags) + .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id) + .ToList(); + Assert.Equal(3, songs.Count); + Assert.Equal(2, songs[0].Tags.Count); + Assert.Equal(1, songs[1].Tags.Count); + Assert.Equal(3, songs[2].Tags.Count); + + var songs2 = g.mysql.Select() + .IncludeMany(a => a.Tags, + then => then.IncludeMany(t => t.Songs)) + .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id) + .ToList(); + Assert.Equal(3, songs2.Count); + Assert.Equal(2, songs2[0].Tags.Count); + Assert.Equal(1, songs2[1].Tags.Count); + Assert.Equal(3, songs2[2].Tags.Count); + } } } diff --git a/FreeSql.Tests/Oracle/Curd/OracleSelectTest.cs b/FreeSql.Tests/Oracle/Curd/OracleSelectTest.cs index 5f672bca..3f942d32 100644 --- a/FreeSql.Tests/Oracle/Curd/OracleSelectTest.cs +++ b/FreeSql.Tests/Oracle/Curd/OracleSelectTest.cs @@ -793,5 +793,141 @@ namespace FreeSql.Tests.Oracle { 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() { + var tag1 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_中国" + }; + tag1.Id = (int)g.oracle.Insert(tag1).ExecuteIdentity(); + var tag1_1 = new Tag { + Parent_id = tag1.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_北京" + }; + tag1_1.Id = (int)g.oracle.Insert(tag1_1).ExecuteIdentity(); + var tag1_2 = new Tag { + Parent_id = tag1.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_上海" + }; + tag1_2.Id = (int)g.oracle.Insert(tag1_2).ExecuteIdentity(); + + var tag2 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_美国" + }; + tag2.Id = (int)g.oracle.Insert(tag2).ExecuteIdentity(); + var tag2_1 = new Tag { + Parent_id = tag2.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_纽约" + }; + tag2_1.Id = (int)g.oracle.Insert(tag2_1).ExecuteIdentity(); + var tag2_2 = new Tag { + Parent_id = tag2.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_华盛顿" + }; + tag2_2.Id = (int)g.oracle.Insert(tag2_2).ExecuteIdentity(); + + var tags0 = g.oracle.Select() + .Include(a => a.Parent) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags = g.oracle.Select() + .IncludeMany(a => a.Tags) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags2 = g.oracle.Select() + .IncludeMany(a => a.Tags, + then => then.Include(a => a.Parent).IncludeMany(a => a.Songs)) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags3 = g.oracle.Select() + .IncludeMany(a => a.Tags, + then => then.Include(a => a.Parent).IncludeMany(a => a.Songs).IncludeMany(a => a.Tags)) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + } + + [Fact] + public void Include_ManyToMany() { + + var tag1 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_01_中国" + }; + tag1.Id = (int)g.oracle.Insert(tag1).ExecuteIdentity(); + var tag2 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_02_美国" + }; + tag2.Id = (int)g.oracle.Insert(tag2).ExecuteIdentity(); + var tag3 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_03_日本" + }; + tag3.Id = (int)g.oracle.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.oracle.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.oracle.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.oracle.Insert(song3).ExecuteIdentity(); + + g.oracle.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.oracle.Insert(new Song_tag { Song_id = song2.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.oracle.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.oracle.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag2.Id }).ExecuteAffrows(); + g.oracle.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id }).ExecuteAffrows(); + g.oracle.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id }).ExecuteAffrows(); + + var songs = g.oracle.Select() + .IncludeMany(a => a.Tags) + .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id) + .ToList(); + Assert.Equal(3, songs.Count); + Assert.Equal(2, songs[0].Tags.Count); + Assert.Equal(1, songs[1].Tags.Count); + Assert.Equal(3, songs[2].Tags.Count); + + var songs2 = g.oracle.Select() + .IncludeMany(a => a.Tags, + then => then.IncludeMany(t => t.Songs)) + .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id) + .ToList(); + Assert.Equal(3, songs2.Count); + Assert.Equal(2, songs2[0].Tags.Count); + Assert.Equal(1, songs2[1].Tags.Count); + Assert.Equal(3, songs2[2].Tags.Count); + } } } diff --git a/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs b/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs index 54989f11..e8c39652 100644 --- a/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs +++ b/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs @@ -859,5 +859,141 @@ namespace FreeSql.Tests.PostgreSQL { sql = query.ToSql().Replace("\r\n", ""); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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() { + var tag1 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_中国" + }; + tag1.Id = (int)g.pgsql.Insert(tag1).ExecuteIdentity(); + var tag1_1 = new Tag { + Parent_id = tag1.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_北京" + }; + tag1_1.Id = (int)g.pgsql.Insert(tag1_1).ExecuteIdentity(); + var tag1_2 = new Tag { + Parent_id = tag1.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_上海" + }; + tag1_2.Id = (int)g.pgsql.Insert(tag1_2).ExecuteIdentity(); + + var tag2 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_美国" + }; + tag2.Id = (int)g.pgsql.Insert(tag2).ExecuteIdentity(); + var tag2_1 = new Tag { + Parent_id = tag2.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_纽约" + }; + tag2_1.Id = (int)g.pgsql.Insert(tag2_1).ExecuteIdentity(); + var tag2_2 = new Tag { + Parent_id = tag2.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_华盛顿" + }; + tag2_2.Id = (int)g.pgsql.Insert(tag2_2).ExecuteIdentity(); + + var tags0 = g.pgsql.Select() + .Include(a => a.Parent) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags = g.pgsql.Select() + .IncludeMany(a => a.Tags) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags2 = g.pgsql.Select() + .IncludeMany(a => a.Tags, + then => then.Include(a => a.Parent).IncludeMany(a => a.Songs)) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags3 = g.pgsql.Select() + .IncludeMany(a => a.Tags, + then => then.Include(a => a.Parent).IncludeMany(a => a.Songs).IncludeMany(a => a.Tags)) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + } + + [Fact] + public void Include_ManyToMany() { + + var tag1 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_01_中国" + }; + tag1.Id = (int)g.pgsql.Insert(tag1).ExecuteIdentity(); + var tag2 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_02_美国" + }; + tag2.Id = (int)g.pgsql.Insert(tag2).ExecuteIdentity(); + var tag3 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_03_日本" + }; + tag3.Id = (int)g.pgsql.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.pgsql.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.pgsql.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.pgsql.Insert(song3).ExecuteIdentity(); + + g.pgsql.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.pgsql.Insert(new Song_tag { Song_id = song2.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.pgsql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + g.pgsql.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag2.Id }).ExecuteAffrows(); + g.pgsql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id }).ExecuteAffrows(); + g.pgsql.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id }).ExecuteAffrows(); + + var songs = g.pgsql.Select() + .IncludeMany(a => a.Tags) + .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id) + .ToList(); + Assert.Equal(3, songs.Count); + Assert.Equal(2, songs[0].Tags.Count); + Assert.Equal(1, songs[1].Tags.Count); + Assert.Equal(3, songs[2].Tags.Count); + + var songs2 = g.pgsql.Select() + .IncludeMany(a => a.Tags, + then => then.IncludeMany(t => t.Songs)) + .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id) + .ToList(); + Assert.Equal(3, songs2.Count); + Assert.Equal(2, songs2[0].Tags.Count); + Assert.Equal(1, songs2[1].Tags.Count); + Assert.Equal(3, songs2[2].Tags.Count); + } } } diff --git a/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs b/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs index 0724b550..3f34a318 100644 --- a/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs +++ b/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs @@ -106,11 +106,11 @@ namespace FreeSql.Tests.SqlServer { [Fact] public void Lazy() { - var tags = g.sqlite.Select().Where(a => a.Parent.Name == "xxx") + var tags = _sqlserverFixture.SqlServer.Select().Where(a => a.Parent.Name == "xxx") .LeftJoin(a => a.Parent_id == a.Parent.Id) .ToSql(); - var songs = g.sqlite.Select().Limit(10).ToList(); + var songs = _sqlserverFixture.SqlServer.Select().Limit(10).ToList(); } @@ -790,5 +790,141 @@ namespace FreeSql.Tests.SqlServer { 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() { + var tag1 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_中国" + }; + tag1.Id = (int)_sqlserverFixture.SqlServer.Insert(tag1).ExecuteIdentity(); + var tag1_1 = new Tag { + Parent_id = tag1.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_北京" + }; + tag1_1.Id = (int)_sqlserverFixture.SqlServer.Insert(tag1_1).ExecuteIdentity(); + var tag1_2 = new Tag { + Parent_id = tag1.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_01_上海" + }; + tag1_2.Id = (int)_sqlserverFixture.SqlServer.Insert(tag1_2).ExecuteIdentity(); + + var tag2 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_美国" + }; + tag2.Id = (int)_sqlserverFixture.SqlServer.Insert(tag2).ExecuteIdentity(); + var tag2_1 = new Tag { + Parent_id = tag2.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_纽约" + }; + tag2_1.Id = (int)_sqlserverFixture.SqlServer.Insert(tag2_1).ExecuteIdentity(); + var tag2_2 = new Tag { + Parent_id = tag2.Id, + Ddd = DateTime.Now.Second, + Name = "test_oneToChilds_02_华盛顿" + }; + tag2_2.Id = (int)_sqlserverFixture.SqlServer.Insert(tag2_2).ExecuteIdentity(); + + var tags0 = _sqlserverFixture.SqlServer.Select() + .Include(a => a.Parent) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags = _sqlserverFixture.SqlServer.Select() + .IncludeMany(a => a.Tags) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags2 = _sqlserverFixture.SqlServer.Select() + .IncludeMany(a => a.Tags, + then => then.Include(a => a.Parent).IncludeMany(a => a.Songs)) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + + var tags3 = _sqlserverFixture.SqlServer.Select() + .IncludeMany(a => a.Tags, + then => then.Include(a => a.Parent).IncludeMany(a => a.Songs).IncludeMany(a => a.Tags)) + .Include(a => a.Parent) + .IncludeMany(a => a.Songs) + .Where(a => a.Id == tag1.Id || a.Id == tag2.Id) + .ToList(); + } + + [Fact] + public void Include_ManyToMany() { + + var tag1 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_01_中国" + }; + tag1.Id = (int)_sqlserverFixture.SqlServer.Insert(tag1).ExecuteIdentity(); + var tag2 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_02_美国" + }; + tag2.Id = (int)_sqlserverFixture.SqlServer.Insert(tag2).ExecuteIdentity(); + var tag3 = new Tag { + Ddd = DateTime.Now.Second, + Name = "test_manytoMany_03_日本" + }; + tag3.Id = (int)_sqlserverFixture.SqlServer.Insert(tag3).ExecuteIdentity(); + + var song1 = new Song { + Create_time = DateTime.Now, + Title = "test_manytoMany_01_我是中国人.mp3", + Url = "http://ww.baidu.com/" + }; + song1.Id = (int)_sqlserverFixture.SqlServer.Insert(song1).ExecuteIdentity(); + var song2 = new Song { + Create_time = DateTime.Now, + Title = "test_manytoMany_02_爱你一万年.mp3", + Url = "http://ww.163.com/" + }; + song2.Id = (int)_sqlserverFixture.SqlServer.Insert(song2).ExecuteIdentity(); + var song3 = new Song { + Create_time = DateTime.Now, + Title = "test_manytoMany_03_千年等一回.mp3", + Url = "http://ww.sina.com/" + }; + song3.Id = (int)_sqlserverFixture.SqlServer.Insert(song3).ExecuteIdentity(); + + _sqlserverFixture.SqlServer.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + _sqlserverFixture.SqlServer.Insert(new Song_tag { Song_id = song2.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + _sqlserverFixture.SqlServer.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag1.Id }).ExecuteAffrows(); + _sqlserverFixture.SqlServer.Insert(new Song_tag { Song_id = song1.Id, Tag_id = tag2.Id }).ExecuteAffrows(); + _sqlserverFixture.SqlServer.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag2.Id }).ExecuteAffrows(); + _sqlserverFixture.SqlServer.Insert(new Song_tag { Song_id = song3.Id, Tag_id = tag3.Id }).ExecuteAffrows(); + + var songs = _sqlserverFixture.SqlServer.Select() + .IncludeMany(a => a.Tags) + .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id) + .ToList(); + Assert.Equal(3, songs.Count); + Assert.Equal(2, songs[0].Tags.Count); + Assert.Equal(1, songs[1].Tags.Count); + Assert.Equal(3, songs[2].Tags.Count); + + var songs2 = _sqlserverFixture.SqlServer.Select() + .IncludeMany(a => a.Tags, + then => then.IncludeMany(t => t.Songs)) + .Where(a => a.Id == song1.Id || a.Id == song2.Id || a.Id == song3.Id) + .ToList(); + Assert.Equal(3, songs2.Count); + Assert.Equal(2, songs2[0].Tags.Count); + Assert.Equal(1, songs2[1].Tags.Count); + Assert.Equal(3, songs2[2].Tags.Count); + } } } diff --git a/FreeSql.Tests/UnitTest1.cs b/FreeSql.Tests/UnitTest1.cs index bd548652..2bfea54e 100644 --- a/FreeSql.Tests/UnitTest1.cs +++ b/FreeSql.Tests/UnitTest1.cs @@ -517,7 +517,7 @@ namespace FreeSql.Tests { } - [Table(Name = "xxx", SelectFilter = " a.id > 0")] + [Table(Name = "TestInfoT1", SelectFilter = " a.id > 0")] class TestInfo { [Column(IsIdentity = true, IsPrimary = true)] public int Id { get; set; } @@ -527,6 +527,7 @@ namespace FreeSql.Tests { public DateTime CreateTime { get; set; } } + [Table(Name = "TestTypeInfoT1")] class TestTypeInfo { [Column(IsIdentity = true)] public int Guid { get; set; } @@ -535,6 +536,7 @@ namespace FreeSql.Tests { public string Name { get; set; } } + [Table(Name = "TestTypeParentInfoT1")] class TestTypeParentInfo { [Column(IsIdentity = true)] public int Id { get; set; } diff --git a/FreeSql.Tests/g.cs b/FreeSql.Tests/g.cs index 9f247108..6df85b6c 100644 --- a/FreeSql.Tests/g.cs +++ b/FreeSql.Tests/g.cs @@ -1,4 +1,5 @@ -using System; +using Npgsql; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; @@ -20,7 +21,9 @@ public class g { .Build()); public static IFreeSql mysql => mysqlLazy.Value; - static Lazy pgsqlLazy = new Lazy(() => new FreeSql.FreeSqlBuilder() + static Lazy pgsqlLazy = new Lazy(() => { + NpgsqlConnection.GlobalTypeMapper.UseLegacyPostgis(); + return 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) @@ -32,7 +35,8 @@ public class g { (cmd, traceLog) => { Console.WriteLine(traceLog); }) //监听SQL命令对象,在执行后 - .Build()); + .Build(); + }); public static IFreeSql pgsql => pgsqlLazy.Value; static Lazy oracleLazy = new Lazy(() => new FreeSql.FreeSqlBuilder()