【发布时间】:2018-02-08 11:34:31
【问题描述】:
我正在尝试使用 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 对象中的角色。它只会为 GameModel 中的List<CharacterModel> 生成一个空对象。然而,游戏和角色都在数据库中。
抽象基础模型
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 中仍然没有数据
【问题讨论】:
-
作为抽象基模型的一部分,所有主键都是扩展类中的属性
Id。 -
我不确定继承 PK 是否有效。我也怀疑它是否有价值。
-
我会看看它是否可以在没有它的情况下工作,但 Xamarin 教程使用继承的 PK:developer.xamarin.com/guides/cross-platform/…
-
@NoChance - 没有区别,我已经更新了问题。
-
谢谢。更新。
标签: c# xamarin sqlite-net sqlite-net-extensions