实体框架数据库种子不种子 [英] Entity Framework database seed doesn't seed

查看:309
本文介绍了实体框架数据库种子不种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是很新的EF。我试图重写种子方法我自定义的初始化里面,使用MVC 4。

问题是,当EF创建数据库,我不觉得插进我的管理​​员表任何初始记录。这里是我的code:

 命名空间FP.Domain.Configurations
{
    公共类InitializeSeed:DropCreateDatabaseIfModelChanges< EFDbContext>
    {
        保护覆盖无效的种子(EFDbContext上下文)
        {
            context.Admins.Add(新管理员
            {
                用户名=admin的,
                密码=admin123456
            });
            base.Seed(上下文);
        }
    }
}

这是我的控制器:

 命名空间FP.WebUI.Controllers
{
    公共类AdminController:控制器
    {
        公众的ViewResult登录()
        {
            Database.SetInitializer(新InitializeSeed());
            返回查看();
        }    }
}

这是我的管理​​员实体:

 命名空间FP.Domain.Entities
{
    公共类管理员
    {
        公众诠释ID {搞定;组; }
        公共字符串用户名{获得;组; }
        公共字符串HashedPassword {搞定;组; }        私人字符串_password;
        公共字符串密码
        {
            组
            {
                this._password =价值;
                字节[] = tempSrc ASCIIEncoding.ASCII.GetBytes(_password);
                HashedPassword = Convert.ToBase64String(新SHA256CryptoServiceProvider()ComputeHash(tempSrc));
            }
        }
    }
}


解决方案

您不能保存更改到数据库。需要注意的是种子的默认实现不做任何事情。

尝试增加调用<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.savechanges%28v=vs.103%29.aspx\"相对=nofollow>的SaveChanges 在你的种子。

 保护覆盖无效种子(EFDbContext上下文)
{
    context.Admins.Add(新管理员
    {
        用户名=admin的,
        密码=admin123456
    });
    base.Seed(上下文);
    context.SaveChanges();
}

I'm quite new to EF. I'm trying to override a Seed method inside my custom initializer, using MVC 4.

The problem is when EF creates the database, i don't find any initial records inserted into my Admins table. Here's my code :

namespace FP.Domain.Configurations
{
    public class InitializeSeed : DropCreateDatabaseIfModelChanges<EFDbContext>
    {
        protected override void Seed(EFDbContext context)
        {
            context.Admins.Add(new Admins
            {                
                Username = "admin",
                Password = "admin123456",
            });
            base.Seed(context);
        }
    }
}

And here's my controller :

namespace FP.WebUI.Controllers
{
    public class AdminController : Controller
    {       
        public ViewResult Login()
        {
            Database.SetInitializer(new InitializeSeed());
            return View();
        }

    }
}

And here's my Admins entity :

namespace FP.Domain.Entities
{
    public class Admins
    {
        public int ID { get; set; }
        public string Username { get; set; }
        public string HashedPassword { get; set; }

        private string _password;
        public string Password
        {
            set
            {
                this._password = value;
                byte[] tempSrc = ASCIIEncoding.ASCII.GetBytes(_password);                    
                HashedPassword = Convert.ToBase64String(new SHA256CryptoServiceProvider().ComputeHash(tempSrc));
            }
        }
    }
}

解决方案

You are not saving your changes to the database. Note that the default implementation of Seed does nothing.

Try adding a call to SaveChanges in your Seed.

protected override void Seed(EFDbContext context)
{
    context.Admins.Add(new Admins
    {                
        Username = "admin",
        Password = "admin123456",
    });
    base.Seed(context);
    context.SaveChanges();
}

这篇关于实体框架数据库种子不种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆