【问题标题】:trying to seed my database using code first migrations尝试使用代码优先迁移来播种我的数据库
【发布时间】:2015-06-26 09:06:58
【问题描述】:

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

这是我的代码:

数据库上下文:

    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 });



        }
    }
}

【问题讨论】:

  • 以后请使用asp.net-mvc-5标签; mvc5 一个空标签,应该很快就会被删除。

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


【解决方案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
        }

【讨论】:

  • 哇,我真不敢相信我忘了这样做,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 2013-09-16
  • 1970-01-01
  • 2016-11-24
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多