Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Last active December 3, 2018 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwill9999/bc115b48e0b7d46300d5bfa1a1da0367 to your computer and use it in GitHub Desktop.
Save jwill9999/bc115b48e0b7d46300d5bfa1a1da0367 to your computer and use it in GitHub Desktop.
Seeding a MySQL database in Visual Studio

Seeding Data into MySQL Database Visual Studio




Where to Call the Function

> In Startup.cs

  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
            
            
            // Call Seeding of Data Here
            SeedData.Initialize(app);
        }

using core1.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;
namespace core1.Data
{
public class SeedData
{
public static void Initialize(IApplicationBuilder app)
{
// Get context out of IApplicationBuilder
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
// check if database has already been created
context.Database.EnsureCreated();
//context.Database.Migrate();
// Look for any teams.
if (context.Teams.Any())
{
return; // DB has already been seeded
}
var teams = SeedData.GetTeams().ToArray();
context.Teams.AddRange(teams);
context.SaveChanges();
var players = SeedData.GetPlayers(context).ToArray();
context.Players.AddRange(players);
context.SaveChanges();
}
}
public static List<Team> GetTeams()
{
var teams = new List<Team>() {
new Team() {
TeamName="Canucks",
City="Vancouver",
Province="BC",
Country="Canada",
},
new Team() {
TeamName="Sharks",
City="San Jose",
Province="CA",
Country="USA",
},
new Team() {
TeamName="Oilers",
City="Edmonton",
Province="AB",
Country="Canada",
},
new Team() {
TeamName="Flames",
City="Calgary",
Province="AB",
Country="Canada",
},
new Team() {
TeamName="Leafs",
City="Toronto",
Province="ON",
Country="Canada",
},
new Team() {
TeamName="Ducks",
City="Anaheim",
Province="CA",
Country="USA",
},
new Team() {
TeamName="Lightening",
City="Tampa Bay",
Province="FL",
Country="USA",
},
new Team() {
TeamName="Blackhawks",
City="Chicago",
Province="IL",
Country="USA",
},
};
return teams;
}
public static List<Player> GetPlayers(ApplicationDbContext context)
{
var players = new List<Player>() {
new Player {
FirstName = "Sven",
LastName = "Baertschi",
TeamName = context.Teams.Find("Canucks").TeamName,
Position = "Forward"
},
new Player {
FirstName = "Hendrik",
LastName = "Sedin",
TeamName = context.Teams.Find("Canucks").TeamName,
Position = "Left Wing"
},
new Player {
FirstName = "John",
LastName = "Rooster",
TeamName = context.Teams.Find("Flames").TeamName,
Position = "Right Wing"
},
new Player {
FirstName = "Bob",
LastName = "Plumber",
TeamName = context.Teams.Find("Oilers").TeamName,
Position = "Defense"
},
};
return players;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment