- 增加 DbContext/Repository ManyToMany联级保存功能(之前已支持OneToMany);

This commit is contained in:
28810
2019-09-29 15:02:08 +08:00
parent 7514000490
commit 33cb3e2dae
11 changed files with 435 additions and 58 deletions

View File

@ -1,4 +1,6 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using Xunit;
namespace FreeSql.Tests
@ -277,5 +279,179 @@ namespace FreeSql.Tests
repos.DataFilter.Apply("xxx", a => (a as AddUpdateInfo).Clicks == 2);
Assert.Null(repos.Find(item.Id));
}
[Fact]
public void EnableAddOrUpdateNavigateList_OneToMany()
{
var repo = g.sqlite.GetRepository<Cagetory>();
var cts = new[] {
new Cagetory
{
Name = "<22><><EFBFBD><EFBFBD>1",
Goodss = new List<Goods>(new[]
{
new Goods { Name = "<22><>Ʒ1" },
new Goods { Name = "<22><>Ʒ2" },
new Goods { Name = "<22><>Ʒ3" }
})
},
new Cagetory
{
Name = "<22><><EFBFBD><EFBFBD>2",
Goodss = new List<Goods>(new[]
{
new Goods { Name = "<22><>Ʒ4" },
new Goods { Name = "<22><>Ʒ5" }
})
}
};
repo.Insert(cts);
cts[0].Name = "<22><><EFBFBD><EFBFBD>11";
cts[0].Goodss.Clear();
cts[1].Name = "<22><><EFBFBD><EFBFBD>22";
cts[1].Goodss.Clear();
repo.Update(cts);
cts[0].Name = "<22><><EFBFBD><EFBFBD>111";
cts[0].Goodss.Clear();
cts[0].Goodss.Add(new Goods { Name = "<22><>Ʒ33" });
cts[1].Name = "<22><><EFBFBD><EFBFBD>222";
cts[1].Goodss.Clear();
cts[1].Goodss.Add(new Goods { Name = "<22><>Ʒ55" });
repo.Update(cts);
}
[Table(Name = "EAUNL_OTM_CT")]
class Cagetory
{
public Guid Id { get; set; }
public string Name { get; set; }
[Navigate("CagetoryId")]
public List<Goods> Goodss { get; set; }
}
[Table(Name = "EAUNL_OTM_GD")]
class Goods
{
public Guid Id { get; set; }
public Guid CagetoryId { get; set; }
public string Name { get; set; }
}
[Fact]
public void EnableAddOrUpdateNavigateList_OneToMany_Parent()
{
var repo = g.sqlite.GetRepository<CagetoryParent>();
var cts = new[] {
new CagetoryParent
{
Name = "<22><><EFBFBD><EFBFBD>1",
Childs = new List<CagetoryParent>(new[]
{
new CagetoryParent { Name = "<22><><EFBFBD><EFBFBD>1_1" },
new CagetoryParent { Name = "<22><><EFBFBD><EFBFBD>1_2" },
new CagetoryParent { Name = "<22><><EFBFBD><EFBFBD>1_3" }
})
},
new CagetoryParent
{
Name = "<22><><EFBFBD><EFBFBD>2",
Childs = new List<CagetoryParent>(new[]
{
new CagetoryParent { Name = "<22><><EFBFBD><EFBFBD>2_1" },
new CagetoryParent { Name = "<22><><EFBFBD><EFBFBD>2_2" }
})
}
};
repo.Insert(cts);
cts[0].Name = "<22><><EFBFBD><EFBFBD>11";
cts[0].Childs.Clear();
cts[1].Name = "<22><><EFBFBD><EFBFBD>22";
cts[1].Childs.Clear();
repo.Update(cts);
cts[0].Name = "<22><><EFBFBD><EFBFBD>111";
cts[0].Childs.Clear();
cts[0].Childs.Add(new CagetoryParent { Name = "<22><><EFBFBD><EFBFBD>1_33" });
cts[1].Name = "<22><><EFBFBD><EFBFBD>222";
cts[1].Childs.Clear();
cts[1].Childs.Add(new CagetoryParent { Name = "<22><><EFBFBD><EFBFBD>2_22" });
repo.Update(cts);
}
[Table(Name = "EAUNL_OTMP_CT")]
class CagetoryParent
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid ParentId { get; set; }
[Navigate("ParentId")]
public List<CagetoryParent> Childs { get; set; }
}
[Fact]
public void EnableAddOrUpdateNavigateList_ManyToMany()
{
var tags = new[] {
new Tag { TagName = "<22><><EFBFBD><EFBFBD>" },
new Tag { TagName = "80<38><30>" },
new Tag { TagName = "00<30><30>" },
new Tag { TagName = <><D2A1>" }
};
var ss = new[]
{
new Song
{
Name = "<22><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>.mp3",
Tags = new List<Tag>(new[]
{
tags[0], tags[1]
})
},
new Song
{
Name = "<22><><EFBFBD><EFBFBD>.mp3",
Tags = new List<Tag>(new[]
{
tags[0], tags[2]
})
}
};
var repo = g.sqlite.GetRepository<Song>();
repo.Insert(ss);
ss[0].Name = "<22><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>.mp5";
ss[0].Tags.Clear();
ss[0].Tags.Add(tags[0]);
ss[1].Name = "<22><><EFBFBD><EFBFBD>.mp5";
ss[1].Tags.Clear();
ss[1].Tags.Add(tags[3]);
repo.Update(ss);
ss[0].Name = "<22><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>.mp4";
ss[0].Tags.Clear();
ss[1].Name = "<22><><EFBFBD><EFBFBD>.mp4";
ss[1].Tags.Clear();
repo.Update(ss);
}
[Table(Name = "EAUNL_MTM_SONG")]
class Song
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<Tag> Tags { get; set; }
}
[Table(Name = "EAUNL_MTM_TAG")]
class Tag
{
public Guid Id { get; set; }
public string TagName { get; set; }
public List<Song> Songs { get; set; }
}
[Table(Name = "EAUNL_MTM_SONGTAG")]
class SongTag
{
public Guid SongId { get; set; }
public Song Song { get; set; }
public Guid TagId { get; set; }
public Tag Tag { get; set; }
}
}
}