
我正在尝试使用Xamarin和sqlite-net(扩展)这个特定的应用程序.
我有2个具有OnetoMany关系的课程
class Game{ [PrimaryKey,autoIncrement] public int ID { get; set; } public string name { get; set; } [OnetoMany(CascadeOperations = CascadeOperation.All)] public List<Player> Players { get; set; } public Game() { Players = new List<Player>(); }}class Player{ [PrimaryKey,autoIncrement] public int ID { get; set; } [MaxLength(8)] public string name { get; set; } [ForeignKey(typeof(Game))] public int GameID { get; set; } [ManyToOne] public Game Game { get; set; }} 现在,在我的活动中,我有类似的东西
sqliteConnection db = new sqliteConnection(new sqlite.Net.Platform.XamarinAndroID.sqlitePlatformAndroID(),path); db.Createtable<Player>(); db.Createtable<Game>(); Game game = new Game(); game.name = "StupID game"; Game game2 = new Game(); game2.name = "Fun game"; Game game3 = new Game(); game3.name = "Amazing game"; db.Insert(game); db.Insert(game2); db.Insert(game3); Player player = new Player(); player.name = name; //Getting this from a input fIEld db.Insert(player); Random random = new Random(); player.GameID = random.Next(1,3); Game game = db.Get<Game>(player.GameID); player.Game = game; db.UpdateWithChildren(player); game.Players.Add(player); db.UpdateWithChildren(game);
这一切似乎都有效,并没有给我任何错误.当我调试这个时,我可以看到玩家确实添加了游戏.但是,当我尝试使用以下语句让所有玩家在其他地方使用时,
List<Player> players = db.table<Player>().ToList();
他们突然没有游戏,当我尝试阅读该属性时,我的程序崩溃了.
我用UpdateWithChildren和InsertWithChildren尝试了一些不同的东西,但无济于事.有什么我做错了或是我没有安装的东西或?
我非常感谢你的帮助.
https://bitbucket.org/twincoders/sqlite-net-extensions
它们提供了诸如GetChildren,UpdateWithChildren等方法,您必须在其中使用泛型db.table< T>.方法.
如果您决定使用递归方法,请确保提供可选的递归参数.
因此,你应该能够做到以下几点:
conn.GetWithChildren<Player>(IDentifIEr,recursive: true)
否则,请阅读上面链接的文档的Cascade Operations部分.
总结以上是内存溢出为你收集整理的c# – 似乎无法使OneToMany关系正常工作全部内容,希望文章能够帮你解决c# – 似乎无法使OneToMany关系正常工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)