.net EF无法使用迁移播种完整数据

时间:2014-03-25 02:32:16

标签: .net entity-framework

我试图播种一些

context.Wishlists.AddOrUpdate(
            w => w.Name,
            new Wishlist
            {
                Id = 1,
                Name = "My First Wishlist",
                Invitees = new List<Invitee> {
                    new Invitee { Id = 1, FirstName = "Super", LastName = "Man", Email = "test@test.com" },
                    new Invitee { Id = 2, FirstName = "Super", LastName = "Bitch", Email = "test2@test2.com" }
                },
                Status = WishlistStatuses.Active,
                Items = new List<Item> { 
                    new Item { Id = 1, Name = "TestItem1", Status = ItemStatuses.Active, Store = "Toys R Us", Description = "Buy the brown one", SuggestedPrice = 100 },
                    new Item { Id = 2, Name = "TestItem2", Status = ItemStatuses.Active },
                    new Item { Id = 3, Name = "TestItem3", Status = ItemStatuses.Reserved }
                },
                Event = new Event { Id = 1, Name = "Shower", When = DateTime.Now, Location = "Here" },
                DateCreated = DateTime.Now
            }
        );

        // if admin user not created
        if (!context.Users.Any(u => u.UserName == "Admin"))
        {
            var userStore = new UserStore<User>(context);
            var userManager = new UserManager<User>(userStore);
            var user = new User
            {
                UserName = "Admin",
                FirstName = "John",
                LastName = "Doe",
                Email = "admin@admin.com",
                Wishlist = context.Wishlists.FirstOrDefault(x => x.Id == 1),
                DateCreated = DateTime.Now
            };

            userManager.Create(user, "password");
        }

问题是user.Wishlist总是为null,无论我做什么。用户模型继承自IdentityUser。所有其他领域都很好。我对这个迁移的东西感到疯狂。 DBInitializer非常简单。

此外,当我尝试重新更新数据库时,我遇到了datetime2的问题,无法更新任何内容。当我在种子中注释掉所有内容时,它只会显示datetime2错误。

1 个答案:

答案 0 :(得分:0)

我能够在我的CreateTable代码中使用c.DateTime(storeType:“datetime2”,nullable:false)修复我的datetime2问题。