示例#1
0
        //creating seed method to create the tables from catalog context
        public static void OnSeed(CatalogContext context)
        {
            //we are migrating the c# code in to the tables in the db
            context.Database.Migrate();

            //adding the data into the tables only if the data is not available
            if (!context.CatalogBrands.Any())
            {
                //add range is like adding data into the tables
                context.CatalogBrands.AddRange(GetPreConfiguredCatalogBrand());
                //saving the changes in our table after adding the content
                context.SaveChanges();
            }
            //if there is no data in the catalog types then adding the data into the table
            if (!context.CatalogTypes.Any())
            {
                context.CatalogTypes.AddRange(GetPreConfiguredCatalogType());
                context.SaveChanges();
            }

            //if there is no data in the catalog items then adding the data into the table
            if (!context.CatalogItems.Any())
            {
                context.CatalogItems.AddRange(GetPreConfiguredCatalogItems());
                context.SaveChanges();
            }
        }
示例#2
0
        // contact with data(CatalogContext)
        public static void Seed(CatalogContext context)
        {
            // before writing the data check migration is available
            // or my table or schema is ready
            context.Database.Migrate();

            // don't want to add data multiple times so check if there is no data in the table
            // context is your DB, CatalogBrands is the table
            if (!context.CatalogBrands.Any())
            {
                // add multiple rows of data by using AddRange()
                context.CatalogBrands.AddRange(GetPreConfiguredCatalogBrands());
                // commit data
                context.SaveChanges();
            }

            if (!context.CatalogTypes.Any())
            {
                context.CatalogTypes.AddRange(GetPreConfiguredCatalogTypes());
                context.SaveChanges();
            }

            if (!context.CatalogItems.Any())
            {
                context.CatalogItems.AddRange(GetPreConfiguredCatalogItems());
                context.SaveChanges();
            }
        }
示例#3
0
        // Powershell is the C# scripting language
        // Need to run two Powershell commands to make Entity Framework work
        // Already:
        // Wrote classes (CatalogBrand, CatalogItem, CatalogType)
        // And DbContext (CatalogContext)
        // Powershell script is what causes the conversion path to the db
        // 1. AddMigration
        // Takes your rules that you specified in your DbContext
        // Converts it into SQL
        // Will do deltas
        // First time is everything

        // 2. UpdateDatabase
        // Takes the migration (from step 1) and actually push to db (create tables, etc.)

        // Need to do these two commands again each time your Domain classes
        // or DbContext change

        // Automation of these sounds convenient

        // Conceptually AddMigration as manual step is important because
        // what if you are developing something and it is not ready yet
        // for the database

        // UpdateDatabase, though, that could be automated
        // After you have run AddMigration, it is safe to do
        // Put this at the start of the Seed method:


        // Pass in the context to the catalog, that is a reference to the db
        // so this knows where to populate the data
        public static void Seed(CatalogContext context)
        // A CatalogContext is a connection to a db
        {
            // This is the same as UpdateDatabase Powershell command
            // If there are migrations wating to be sent to the database, send them
            context.Database.Migrate();
            // Check for pending migrations and push them
            // If no pending migrations exist, do nothing

            // In production, the Migrate() call (UpdateDatabase)
            // is the only thing here in Seed() that you'll do
            // Everything else is just for class purposes.


            // Populate the tables with data
            if (!context.CatalogBrands.Any())
            // Any is a LINQ query...are there any records?
            // If there are no rows in the table
            {
                // Only then do we seed the table with data

                // Recall the three tables DbSet<> in CatalogContext : DbContext
                // CatalogBrands, CatalogTypes and CatalogItems

                // This is how you can talk to your table from code:
                context.CatalogBrands.AddRange(GetPreconfiguredCatalogBrands());
                // Just one line in Entity Framework (not 10 lines of SQL query)
                // .Add() for one row, .AddRange() for multiple rows

                // Must save, otherwise changes not committed
                context.SaveChanges();

                // SaveChanges() seperate command so that you can group/bulk add
                // Then save as a final step
                // But in this case, saving before adding CatalogItems is important,
                // because CatalogItems has dependencies on CatalogBrands and
                // CatalogTypes
            }
            if (!context.CatalogTypes.Any())
            {
                context.CatalogTypes.AddRange(GetPreconfiguredCatalogTypes());
                context.SaveChanges();
            }
            if (!context.CatalogItems.Any())
            {
                context.CatalogItems.AddRange(GetPreconfiguredCatalogItems());
                context.SaveChanges();
            }
        }
 public static void Seed(CatalogContext context)
 {
     context.Database.Migrate();
     if (!context.CatalogBrands.Any())
     {
         context.CatalogBrands.AddRange(GetPreConfiguredCatalogBrands());
         context.SaveChanges();
     }
     if (!context.CatalogTypes.Any())
     {
         context.CatalogTypes.AddRange(GetPreConfiguredCatalogTypes());
         context.SaveChanges();
     }
     if (!context.CatalogItems.Any())
     {
         context.CatalogItems.AddRange(GetPreConfiguredCatalogItems());
         context.SaveChanges();
     }
 }
 //Populate my db table with some sample data - Module 11 - talking about static - 1 global instance for everyone to use
 // here catalog context refers to the connection to the database - where we need to populate the data
 // all methods should be static -seed it with data (using powershell)
 // Entity framework to work we need to run 2 power shell commands
 // pathway 1 . add migration is to take context defn and make a sql syntax to translate c# to sql command
 // 1st time all the data has to be migrated, 2nd time would be update migration to the database
 public static void Seed(CatalogContext context)
 {
     // Here only update migration is automated
     context.Database.Migrate();
     if (!context.CatalogBrands.Any())
     {
         context.CatalogBrands.AddRange(GetPreConfiguredCatalogBrands());
         context.SaveChanges();  // Very important to commit the changes
     }
     if (!context.CatalogTypes.Any())
     {
         context.CatalogTypes.AddRange(GetPreConfiguredCatalogTypes());
         context.SaveChanges();
     }
     if (!context.CatalogItems.Any())
     {
         context.CatalogItems.AddRange(GetPreConfiguredCatalogItems());
         context.SaveChanges();
     }
 }
示例#6
0
        public static void Seed(CatalogContext context)

        {
            context.Database.Migrate();

            if (!context.CatalogEventCategories.Any())

            {
                context.CatalogEventCategories.AddRange(GetPreConfiguredCatalogEventCategories());

                context.SaveChanges();
            }

            if (!context.CatalogEventTypes.Any())

            {
                context.CatalogEventTypes.AddRange(GetPreConfiguredCatalogEventTypes());

                context.SaveChanges();
            }

            if (!context.CatalogEventLocations.Any())

            {
                context.CatalogEventLocations.AddRange(GetPreConfiguredCatalogEventLocations());

                context.SaveChanges();
            }

            if (!context.CatalogEventItems.Any())

            {
                context.CatalogEventItems.AddRange(GetPreConfiguredCatalogEventItems());

                context.SaveChanges();
            }
        }