public void SmartSaveMListOrder() { using (Transaction tr = new Transaction()) using (OperationLogic.AllowSave <AlbumEntity>()) using (OperationLogic.AllowSave <ArtistEntity>()) { var artist = Database.Query <ArtistEntity>().First(); var album = new AlbumEntity { Name = "Test album", Author = artist, Year = 2000, Songs = { new SongEntity { Name = "Song 0" }, new SongEntity { Name = "Song 1" }, new SongEntity { Name = "Song 2" }, }, State = AlbumState.Saved, }; album.Save(); AssertSequenceEquals(album.MListElements(a => a.Songs).OrderBy(a => a.Order).Select(mle => KVP.Create(mle.Order, mle.Element.Name)), new Dictionary <int, string> { { 0, "Song 0" }, { 1, "Song 1" }, { 2, "Song 2" } }); var ids = album.MListElements(a => a.Songs).Select(a => a.RowId).ToHashSet(); album.Songs.SortDescending(a => a.Name); album.Save(); var ids2 = album.MListElements(a => a.Songs).Select(a => a.RowId).ToHashSet(); AssertSequenceEquals(ids.OrderBy(), ids2.OrderBy()); AssertSequenceEquals(album.MListElements(a => a.Songs).OrderBy(a => a.Order).Select(mle => KVP.Create(mle.Order, mle.Element.Name)), new Dictionary <int, string> { { 0, "Song 2" }, { 1, "Song 1" }, { 2, "Song 0" } }); var s3 = album.Songs[0]; album.Songs.RemoveAt(0); album.Songs.Insert(1, s3); album.Save(); AssertSequenceEquals(album.MListElements(a => a.Songs).OrderBy(a => a.Order).Select(mle => KVP.Create(mle.Order, mle.Element.Name)), new Dictionary <int, string> { { 0, "Song 1" }, { 1, "Song 2" }, { 2, "Song 0" } }); AssertSequenceEquals(album.ToLite().Retrieve().Songs.Select(a => a.Name), new[] { "Song 1", "Song 2", "Song 0" }); //tr.Commit(); } }
public void SmartSaveMList() { using (Transaction tr = new Transaction()) using (OperationLogic.AllowSave <AlbumEntity>()) using (OperationLogic.AllowSave <ArtistEntity>()) { var maxRowId = Database.MListQuery((AlbumEntity a) => a.Songs).Max(a => a.RowId); var artist = Database.Query <ArtistEntity>().First(); var album = new AlbumEntity { Name = "Test album", Author = artist, Year = 2000, Songs = { new SongEntity { Name = "Song 1" } }, State = AlbumState.Saved, }; var innerList = ((IMListPrivate <SongEntity>)album.Songs).InnerList; Assert.IsNull(innerList[0].RowId); //Insert and row-id is set album.Save(); Assert.IsNotNull(innerList[0].RowId); Assert.IsTrue(innerList[0].RowId > maxRowId); album.Songs.Add(new SongEntity { Name = "Song 2" }); Assert.IsNull(innerList[1].RowId); album.Save(); //Insert and row-id is set Assert.IsNotNull(innerList[1].RowId); var song = innerList[0]; album.Songs.Remove(song.Element); //Delete album.Save(); { var album2 = album.ToLite().Retrieve(); Assert.IsTrue(album.Songs.Count == album2.Songs.Count); Assert.IsTrue(innerList[0].RowId == ((IMListPrivate <SongEntity>)album2.Songs).InnerList[0].RowId); Assert.IsTrue(!album.MListElements(a => a.Songs).Any(mle => mle.RowId == song.RowId)); } album.Songs[0].Name += "*"; //Update album.Save(); { var album2 = album.ToLite().Retrieve(); Assert.IsTrue(album.Songs.Count == album2.Songs.Count); Assert.IsTrue(innerList[0].RowId == ((IMListPrivate <SongEntity>)album2.Songs).InnerList[0].RowId); Assert.IsTrue(album.Songs[0].Name == album2.Songs[0].Name); Assert.IsTrue(!album.MListElements(a => a.Songs).Any(mle => mle.RowId == song.RowId)); } //tr.Commit(); } }