コード例 #1
0
        public static void Initialize(SimpleApiDBContext context)
        {
            context.Database.EnsureCreated();

            if (context.DataItems.Any())
            {
                return; // Database has been populated.
            }

            // populate the database
            Models.DataItem[] dataItems = new Models.DataItem[]
            {
                new Models.DataItem()
                {
                    Id = "FirstItem", Value = "This is an item added by the Database initialization."
                }
            };
            foreach (Models.DataItem dataItem in dataItems)
            {
                context.DataItems.Add(dataItem);
            }


            context.SaveChanges();
        }
コード例 #2
0
 private static void InitializeDatabase(IHost host)
 {
     using (IServiceScope scope = host.Services.CreateScope())
     {
         IServiceProvider services = scope.ServiceProvider;
         try
         {
             Data.SimpleApiDBContext context = services.GetRequiredService <Data.SimpleApiDBContext>();
             Data.DatabaseInitializer.Initialize(context);
         }
         catch (Exception exception)
         {
             ILogger logger = services.GetRequiredService <ILogger <Program> >();
             logger.LogError(exception, "An error occurred while creating the Database.");
         }
     }
 }