public PreKeyModel ReplaceOmemoPreKey(PreKeyModel preKey) { using (MainDbContext ctx = new MainDbContext()) { // Remove the old key: dbAccount.omemoInfo.preKeys.Remove(preKey); preKey.Remove(ctx, true); // Generate a new one: PreKeyModel newPreKey = KeyHelper.GeneratePreKey(dbAccount.omemoInfo.maxPreKeyId++); dbAccount.omemoInfo.preKeys.Add(newPreKey); dbAccount.omemoInfo.bundleInfoAnnounced = false; // Store everything in the DB: ctx.Add(newPreKey); ctx.Update(dbAccount.omemoInfo); return(newPreKey); } }