/// <summary> /// This method either retrieves the default Character associated with this account or /// creates a new character to be that default. /// </summary> /// <param name="acct">The user's account whose character we are trying to retrieve.</param> /// <returns></returns> private Character GetOrCreateCharacter(Account acct) { Character chr = default(Character); if (acct.ActiveCharacter != null) { chr = acct.ActiveCharacter; } else { using (RPGBotDbContext context = new RPGBotDbContext()) { context.Attach <Account>(acct); chr = new Character() { Account = acct, AccountID = acct.AccountID, Experience = 0, Level = 1, IsActiveCharacter = true, Name = "[Character Name]" }; context.Characters.Add(chr); context.SaveChanges(); } } return(chr); }
public void TestRPGBotDbContext_AddAccount() { Random rand = new Random(); byte[] idBytes = new byte[8]; rand.NextBytes(idBytes); long newID = BitConverter.ToInt64(idBytes, 0); EntityState predictedEntityState = EntityState.Unchanged; EntityState actualEntityState = EntityState.Detached; using (RPGBotDbContext context = new RPGBotDbContext()) { Account newAcct = new Account() { AccountID = newID }; context.Accounts.Add(newAcct); context.SaveChanges(); actualEntityState = context.Entry <Account>(newAcct).State; } Assert.AreEqual(predictedEntityState, actualEntityState); }
/// <summary> /// This method either retrieves an existing Account record or creates an Account. /// </summary> /// <param name="target">The Discord target whose Account we are trying to retrieve.</param> /// <returns></returns> private Account GetOrCreateAccount(SocketUser target) { Account acct = default(Account); using (RPGBotDbContext context = new RPGBotDbContext()) { // We have to cast the Discord Id to a long because Entity Framework/Sql Server does not support unsigned integers. acct = context.Accounts .Include(x => x.Characters) .SingleOrDefault(x => x.AccountID == (long)target.Id); if (acct == null) { acct = new Account() { AccountID = (long)target.Id, }; context.Accounts.Add(acct); context.SaveChanges(); } } return(acct); }