MVC3中CodeFirst数据库初始化数据添加失败
Model用户类
public class User
{
[Key]
[StringLength(50)]
[DisplayName("用户编号")]
public string UserCd { get; set; }
/// <summary>
/// role表中的主键,角色编号
/// </summary>
//[Required(ErrorMessage="用户角色不能为空")]
//public int RoleId { get; set; }
/// <summary>
/// 部门编号
/// </summary>
//[Required(ErrorMessage = "部门不能为空")]
//public int GroupId { get; set; }
/// <summary>
/// 姓名
/// </summary>
[Required(ErrorMessage = "姓名不能为空")]
[StringLength(50)]
public string UserName { get; set; }
/// <summary>
/// 身份证
/// </summary>
[Required(ErrorMessage = "身份证不能空")]
[StringLength(50)]
public string UserIdCode { get; set; }
/// <summary>
/// false-女,true-男
/// </summary>
[Required(ErrorMessage = "用户性别")]
[DefaultValue(false)]
public bool UserSex { get; set; }
[Required(ErrorMessage = "用户密码不能为空")]
[StringLength(50, MinimumLength = 6)]
[DataType(DataType.Password)]
public string UserPassword { get; set; }
/// <summary>
/// 用户照片
/// </summary>
public string UserPic { get; set; }
/// <summary>
/// 用户最后一次登录时间
/// </summary>
//[DefaultValue(System.DateTime.Now.ToString())]
public System.DateTime UserLoginTime { get; set; }
/// <summary>
/// 最后一次登录地址
/// </summary>
public string UserLoginIp { get; set; }
/// <summary>
/// 在线状态true在线,false下线
/// </summary>
[DefaultValue(false)]
public bool UserOnline { get; set; }
/// <summary>
/// false-没有删除,true删除
/// </summary>
[DefaultValue(false)]
public bool Del { get; set; }
}
public class DbDescribe
{
/// <summary>
/// 主键
/// </summary>
[Key]
[ScaffoldColumn(false)]
[StringLength(50)]
public string TableCd { get; set; }
/// <summary>
/// 表名称
/// </summary>
[ScaffoldColumn(false)]
[StringLength(50)]
public string TableName { get; set; }
[DisplayName("表描述")]
public string TableDescribe { get; set; }
}
/// <summary>
/// 数据库
/// </summary>
public class CrmSystemEntities:DbContext
{
/// <summary>
/// 数据库表
/// </summary>
public DbSet<DbDescribe> DbDescrible { get; set; }
/// <summary>
/// 用户表
/// </summary>
public DbSet<User> Users { get; set; }
}
public class CrmSystemDataInitializer:DropCreateDatabaseIfModelChanges<CrmSystemEntities>
{
protected override void Seed(CrmSystemEntities context)
{
var users = new List<User>
{
new User{ UserCd = "2006001",UserName="ChenHua",UserIdCode="341000198308068390",
UserSex=false,UserPassword="123456",UserLoginTime=System.Convert.ToDateTime("2012-12-22 0:1"),
UserLoginIp="127.0.0.1"} ,
new User{ UserCd = "2006002",UserName="ZhongWenLong",UserIdCode="610203197406077960",
UserSex=true,UserPassword="123456",UserLoginTime=System.Convert.ToDateTime("2012-12-22 0:1"),
UserLoginIp="127.0.0.1"
}
};
var dbDescribes = new List<DbDescribe>
{
new DbDescribe{ TableCd="01",TableName="Users",TableDescribe="用户表"}
};
//将数据加入
users.ForEach(a => context.Users.Add(a));
//将数据加入
dbDescribes.ForEach(a => context.DbDescrible.Add(a));
}
}
}
protected void Application_Start()
{
//初始化数据库
System.Data.Entity.Database.SetInitializer(new ZQHCRMSYSTEM.Models.CrmSystemDataInitializer());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}