Christopher Griffin
Christopher Griffin

Reputation: 13

trying to seed my database using code first migrations

im trying to seed my database using code first migrations MVC5 i enabled migrations and added migration but when i update database it tells me that my object is not set to an instance of an object when it is. the funny thing is that when i update database a second time it works.

here is my code:

dbcontext:

    using BrokeMans.Data.Models;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrokeMans.Data
{
    public class BrokeContext : IdentityDbContext<ApplicationUser>
    {
        public BrokeContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
        public DbSet<Item> Items { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public static BrokeContext Create()
        {
            return new BrokeContext();
        }

    }

}
 configuration:
--------------------------------------------------------------------------------
namespace BrokeMans.Data.Migrations
{
    using BrokeMans.Data.Models;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    internal sealed class Configuration : DbMigrationsConfiguration<BrokeMans.Data.BrokeContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(BrokeContext context)
        {
            UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
            UserManager<ApplicationUser> manager = new UserManager<ApplicationUser>(store);

            RoleStore<Role> roleStore = new RoleStore<Role>(context);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);

            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new Role
                {
                    Name = "Admin"
                });
            }
            if (!roleManager.RoleExists("User"))
            {
                roleManager.Create(new Role
                {
                    Name = "User"
                });
            }
            ApplicationUser Chris = manager.FindByName("Chris");
            if (Chris == null)
            {
                manager.Create(new ApplicationUser
                {
                    UserName = "Chris",
                    Email = "[email protected]",
                }, "123456");
            }
            ApplicationUser Tiff = manager.FindByName("Tiff");
            if (Tiff == null)
            {
                manager.Create(new ApplicationUser
                {
                    UserName = "Tiff",
                    Email = "[email protected]"
                }, "123456");
            }
            context.Items.AddOrUpdate(i => i.Id,
                new Item { Id = 1, UserId = Chris.Id, Title = "RAMEN", Pic = "later", Description = "this is cheap but okay" },
                new Item { Id = 2, UserId = Tiff.Id, Title = "Chicken Ramin", Pic = "later", Description = "this is better than the original Ramen" },
                new Item { Id = 3, UserId = Tiff.Id, Title = "Beef Ramin", Pic = "later", Description = "this is better than the Chicken Ramen" },
                new Item { Id = 4, UserId = Chris.Id, Title = "Spicy Ramin", Pic = "later", Description = "this is better than the Beef Ramen" }
                );
            context.Comments.AddOrUpdate(c => c.Id,
                new Comment { Id = 1, ItemId = 3, UserComment = "this is from chris on Beef", UserId = Chris.Id, DateCreated = DateTime.UtcNow },
                new Comment { Id = 1, ItemId = 2, UserComment = "this is from chris on Chicken", UserId = Chris.Id, DateCreated = DateTime.UtcNow },
                new Comment { Id = 1, ItemId = 1, UserComment = "this is from tiff on ramen", UserId = Tiff.Id, DateCreated = DateTime.UtcNow },
                new Comment { Id = 1, ItemId = 4, UserComment = "this is from tiff on spicy", UserId = Tiff.Id, DateCreated = DateTime.UtcNow });



        }
    }
}

Upvotes: 1

Views: 105

Answers (1)

Pablo Romeo
Pablo Romeo

Reputation: 11396

You don't seem to be setting the Chris or Tiff variables to the newly created ones, and at a later stage you try to use Chris.Id and Tiff.Id, which would cause a Null Reference Exception.

It also explains why it works when running a second time, given that the users do exist the second time around.

You can solve that by calling FindByName after you create the users:

        ApplicationUser Chris = manager.FindByName("Chris");
        if (Chris == null)
        {
            manager.Create(new ApplicationUser
            {
                UserName = "Chris",
                Email = "[email protected]",
            }, "123456");
            Chris = manager.FindByName("Chris");    //update reference
        }
        ApplicationUser Tiff = manager.FindByName("Tiff");
        if (Tiff == null)
        {
            manager.Create(new ApplicationUser
            {
                UserName = "Tiff",
                Email = "[email protected]"
            }, "123456");
            Tiff = manager.FindByName("Tiff");    //update reference
        }

Upvotes: 1

Related Questions