Beispiel #1
0
        /// <summary>
        /// Seeds the context.
        /// </summary>
        /// <param name="context">The context.</param>
        private async Task SeedContextAsync(ILocalDbContext context)
        {
            // Prepare accounts
            if (!context.Accounts.Any())
            {
                var account = new Account
                {
                    Name = "Account"
                };

                context.Accounts.Add(account);
                await context.SaveChangesAsync();
            }

            // Prepare groups
            if (!context.Groups.Any())
            {
                var shoppingCategory = new Group
                {
                    Name      = "Shopping",
                    AccountId = context.Accounts.First().Id
                };

                context.Groups.Add(shoppingCategory);
                await context.SaveChangesAsync();
            }

            // Prepare categories
            if (!context.Categories.Any())
            {
                var foodCategory = new Category
                {
                    Color   = Colors.Purple.ToString(),
                    Name    = "Food",
                    GroupId = context.Groups.First().Id
                };

                context.Categories.Add(foodCategory);
                await context.SaveChangesAsync();
            }
        }
        /// <summary>
        /// Saves the context.
        /// </summary>
        /// <returns>Returns <c>True</c> if context was saved successfully; <c>False</c> otherwise.</returns>
        public static async Task <bool> TrySaveContextAsync(this ILocalDbContext context)
        {
            try
            {
                await context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                var logger = PrismUnityApplication.Current.Container.Resolve <ILogger>();
                logger.Error(ex, "Failed to save context changes.");
                return(false);
            }
        }