这是我的两个实体和上下文类:
public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
}
public class Event
{
public Guid Id { get; set; }
[Required]
public string Header { get; set; }
[Required]
public string Description { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public ApplicationUser Host { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Event> Events { get; set; }
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
这是我的种子方法:
protected override void Seed(ApplicationDbContext context)
{
var manager = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
new ApplicationDbContext()));
var user0 = new ApplicationUser()
{
UserName = "User0",
FirstName = "FirstName0",
LastName = "LastName0",
Email = "Email0@Example.com",
};
manager.Create(user0, "Password0");
var user1 = new ApplicationUser()
{
UserName = "User1",
FirstName = "FirstName1",
LastName = "LastName1",
Email = "Email1@Example.com",
};
manager.Create(user1, "Password1");
var user2 = new ApplicationUser()
{
UserName = "User2",
FirstName = "FirstName2",
LastName = "LastName2",
Email = "Email2@Example.com",
};
var eventObj = new Event()
{
Id = Guid.NewGuid(),
Host = user0,
Header = "Header0",
Description = "Description0",
Date = DateTime.Now
};
manager.Create(user2, "Password2");
context.Events.AddOrUpdate(eventObj);
}
当我在Package Manager Console中运行命令 Update-Database 时,我得到以下结果:
一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。
我做错了什么?我正在研究VS 2013 Asp.net Mvc模板。
提前致谢,
答案 0 :(得分:0)
将context
参数传递到您的UserStore<ApplicationUser>
构造函数中(您需要为用户使用与DbContext
方法稍后用于事件的Seed
实例相同的protected override void Seed(ApplicationDbContext context)
{
var manager = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
// new ApplicationDbContext() // <- no
context // yes!
));
实例):
{{1}}
答案 1 :(得分:0)
如果数据已经存在,那么请考虑以这种方式使用addorupdate。
种子数据:覆盖此类中的Seed方法以添加种子数据。 - 迁移到最新版本后将调用Seed方法。 - 您可以使用DbContext.AddOrUpdate()辅助扩展方法来避免创建重复的种子数据。 E.g。
myContext.AddOrUpdate(c =&gt; c.FullName, 新客户{FullName =“Andrew Peters”,CustomerNumber = 123}, );
答案 2 :(得分:0)
我改变了种子方法,现在工作正常:
var manager = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
context));
var user0 = new ApplicationUser()
{
UserName = "User0",
FirstName = "FirstName0",
LastName = "LastName0",
Email = "Email0@Example.com",
};
manager.Create(user0, "Password0");
var user1 = new ApplicationUser()
{
UserName = "User1",
FirstName = "FirstName1",
LastName = "LastName1",
Email = "Email1@Example.com",
};
manager.Create(user1, "Password1");
var user2 = new ApplicationUser()
{
UserName = "User2",
FirstName = "FirstName2",
LastName = "LastName2",
Email = "Email2@Example.com",
};
var eventObj = new Event()
{
Id = Guid.NewGuid(),
Host = user2,
Header = "Header0",
Description = "Description0",
Date = DateTime.Now
};
context.Events.AddOrUpdate(eventObj);
manager.Create(user2, "Password2");
现在我在将Event实体添加到上下文后调用Create user方法。