增加其他数据库,Include 单元测试

This commit is contained in:
28810 2019-05-11 17:28:58 +08:00
parent dbfc33fef5
commit be0f48b629
7 changed files with 557 additions and 7 deletions

View File

@ -35,7 +35,7 @@ namespace FreeSql.Tests.DataContext.SqlServer
SqlServer.Ado.ExecuteNonQuery("DROP SCHEMA dbo2"); 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", "tb_topic22211", "tb_topic111333", "TestTypeInfo", "TestTypeInfo333", "TestTypeParentInfo",
"TestTypeParentInfo23123", "xxdkdkdk1222", "xxx"}; "TestTypeParentInfo23123", "xxdkdkdk1222", "xxx"};
foreach (var tempTable in tempTables) foreach (var tempTable in tempTables)

View File

@ -900,5 +900,141 @@ namespace FreeSql.Tests.MySql {
sql = query.ToSql().Replace("\r\n", ""); 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); 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<Tag>()
.Include(a => a.Parent)
.Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
.ToList();
var tags = g.mysql.Select<Tag>()
.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<Tag>()
.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<Tag>()
.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<Song>()
.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<Song>()
.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);
}
} }
} }

View File

@ -793,5 +793,141 @@ namespace FreeSql.Tests.Oracle {
sql = query.ToSql().Replace("\r\n", ""); 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); 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<Tag>()
.Include(a => a.Parent)
.Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
.ToList();
var tags = g.oracle.Select<Tag>()
.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<Tag>()
.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<Tag>()
.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<Song>()
.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<Song>()
.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);
}
} }
} }

View File

@ -859,5 +859,141 @@ namespace FreeSql.Tests.PostgreSQL {
sql = query.ToSql().Replace("\r\n", ""); 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); 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<Tag>()
.Include(a => a.Parent)
.Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
.ToList();
var tags = g.pgsql.Select<Tag>()
.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<Tag>()
.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<Tag>()
.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<Song>()
.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<Song>()
.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);
}
} }
} }

View File

@ -106,11 +106,11 @@ namespace FreeSql.Tests.SqlServer {
[Fact] [Fact]
public void Lazy() { public void Lazy() {
var tags = g.sqlite.Select<Tag>().Where(a => a.Parent.Name == "xxx") var tags = _sqlserverFixture.SqlServer.Select<Tag>().Where(a => a.Parent.Name == "xxx")
.LeftJoin(a => a.Parent_id == a.Parent.Id) .LeftJoin(a => a.Parent_id == a.Parent.Id)
.ToSql(); .ToSql();
var songs = g.sqlite.Select<Song>().Limit(10).ToList(); var songs = _sqlserverFixture.SqlServer.Select<Song>().Limit(10).ToList();
} }
@ -790,5 +790,141 @@ namespace FreeSql.Tests.SqlServer {
sql = query.ToSql().Replace("\r\n", ""); 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); 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<Tag>()
.Include(a => a.Parent)
.Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
.ToList();
var tags = _sqlserverFixture.SqlServer.Select<Tag>()
.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<Tag>()
.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<Tag>()
.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<Song>()
.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<Song>()
.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);
}
} }
} }

View File

@ -517,7 +517,7 @@ namespace FreeSql.Tests {
} }
[Table(Name = "xxx", SelectFilter = " a.id > 0")] [Table(Name = "TestInfoT1", SelectFilter = " a.id > 0")]
class TestInfo { class TestInfo {
[Column(IsIdentity = true, IsPrimary = true)] [Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; } public int Id { get; set; }
@ -527,6 +527,7 @@ namespace FreeSql.Tests {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
[Table(Name = "TestTypeInfoT1")]
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)] [Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
@ -535,6 +536,7 @@ namespace FreeSql.Tests {
public string Name { get; set; } public string Name { get; set; }
} }
[Table(Name = "TestTypeParentInfoT1")]
class TestTypeParentInfo { class TestTypeParentInfo {
[Column(IsIdentity = true)] [Column(IsIdentity = true)]
public int Id { get; set; } public int Id { get; set; }

View File

@ -1,4 +1,5 @@
using System; using Npgsql;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
@ -20,7 +21,9 @@ public class g {
.Build()); .Build());
public static IFreeSql mysql => mysqlLazy.Value; public static IFreeSql mysql => mysqlLazy.Value;
static Lazy<IFreeSql> pgsqlLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder() static Lazy<IFreeSql> pgsqlLazy = new Lazy<IFreeSql>(() => {
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") .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) .UseAutoSyncStructure(true)
.UseSyncStructureToLower(true) .UseSyncStructureToLower(true)
@ -32,7 +35,8 @@ public class g {
(cmd, traceLog) => { (cmd, traceLog) => {
Console.WriteLine(traceLog); Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后 }) //监听SQL命令对象在执行后
.Build()); .Build();
});
public static IFreeSql pgsql => pgsqlLazy.Value; public static IFreeSql pgsql => pgsqlLazy.Value;
static Lazy<IFreeSql> oracleLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder() static Lazy<IFreeSql> oracleLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()