我正在尝试使用 SQLite Net Extensions 为游戏做笔记应用程序,它使用 3 层模型,Game [1 has many *] Character [1 has many *] Note [1 applies to *] Character
我在 Visual Studio Community 2015 中使用 Xamarin,并使用 NuGet 包管理器安装了 SQLiteNetExtensions。
我还没有超过 Game 和角色之间的第一级关系,并且插入数据库(无论是通过初始插入然后更新,还是递归使用 InsertWithChildren)不会更新 Game 对象中的 Characters。它只会为List<CharacterModel>
内部 GameModel 生成一个空对象。然而,游戏和角色都在数据库中。
抽象基础模型
public abstract class IdentifiableModel
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
}
游戏模型
[Table("Game")]
public class GameModel : IdentifiableModel
{
[MaxLength(64)]
public string Name { get; set; }
[OneToMany]
public List<CharacterModel> Characters { get; set; }
}
人物模型
[Table("Characters")]
public class CharacterModel : IdentifiableModel
{
[ForeignKey(typeof (GameModel))]
public int GameId { get; set; }
[ManyToOne]
public GameModel Game { get; set; }
public string FullName { get; set; }
public string ShortName { get; set; }
}
为了测试插入数据库,我在我的主要活动中执行此操作:
var game =
new GameModel
{
Name = "Game"
};
database.Insert(game);
var characters = new List<CharacterModel>
{
new CharacterModel
{
FullName = "Dude"
},
new CharacterModel
{
FullName = "Dudette"
}
};
database.InsertAll(characters);
game.Characters = characters;
database.UpdateWithChildren(game);
var testGame = database.GetAll<GameModel>().FirstOrDefault();
var testCharacter = database.GetAll<CharacterModel>().FirstOrDefault();
Console.WriteLine(testGame.Id + " " + testGame.Name);
Console.WriteLine(testCharacter.Id + " " + testCharacter.FullName + " " + testCharacter.GameId);
//testGame.Characters; // THIS IS NULL.
//testCharacter.Game; // THIS IS NULL.
我不知道从哪里开始对此进行排序,希望能得到一些帮助来启动和运行它。
编辑:使用非继承的主键根本没有区别。testGame.Characters
或中仍然没有数据testCharacter.Game