尝试使用代码优先迁移来种子数据库

时间:2015-04-19 18:54:23

标签: linq entity-framework asp.net-mvc-5

我尝试使用代码首先迁移种子数据库MVC5我启用了迁移并添加了迁移但是当我更新数据库时它告诉我我的对象没有设置为对象的实例。有趣的是,当我第二次更新数据库时,它的工作原理。

这是我的代码:

的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 = "cgriffin.902@gmail.com",
                }, "123456");
            }
            ApplicationUser Tiff = manager.FindByName("Tiff");
            if (Tiff == null)
            {
                manager.Create(new ApplicationUser
                {
                    UserName = "Tiff",
                    Email = "daking_234@yahoo.com"
                }, "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 });



        }
    }
}

1 个答案:

答案 0 :(得分:1)

您似乎没有将Chris或Tiff变量设置为新创建的变量,并且稍后您尝试使用Chris.IdTiff.Id,这会导致Null参考例外。

它还解释了为什么它在第二次运行时有效,因为用户确实第二次存在。

您可以在创建用户后调用FindByName来解决此问题:

        ApplicationUser Chris = manager.FindByName("Chris");
        if (Chris == null)
        {
            manager.Create(new ApplicationUser
            {
                UserName = "Chris",
                Email = "cgriffin.902@gmail.com",
            }, "123456");
            Chris = manager.FindByName("Chris");    //update reference
        }
        ApplicationUser Tiff = manager.FindByName("Tiff");
        if (Tiff == null)
        {
            manager.Create(new ApplicationUser
            {
                UserName = "Tiff",
                Email = "daking_234@yahoo.com"
            }, "123456");
            Tiff = manager.FindByName("Tiff");    //update reference
        }