在Configuration.cs的Seed()方法中,我在添加数据时遇到了一些错误:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
栈跟踪
at System.Data.Entity.Internal.InternalContext.SaveChanges()
我的Configuration.cs文件包含种子数据:
namespace MarginWorkbenchNG.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
internal sealed class Configuration : DbMigrationsConfiguration<MarginWorkbenchNG.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true; // added - BM:
ContextKey = "MarginWorkbenchNG.Models.ApplicationDbContext";
}
protected override void Seed(MarginWorkbenchNG.Models.ApplicationDbContext context)
{
var hasher = new PasswordHasher();
// **** Causing "Validation failed" errors on line 80 of AccountController.cs ****
context.Roles.AddOrUpdate(
new IdentityRole { Name = "SystemAdministrator" }
);
context.Users.AddOrUpdate(
new Models.ApplicationUser { Company = "ICAP", EmailConfirmed = true, FirstName = "Admin", LastName = "Demo", UserName = "admin@rzr.com", Email = "admin@rzr-risk.com", PasswordHash = hasher.HashPassword("test123") },
new Models.ApplicationUser { Company = "ICAP", EmailConfirmed = true, FirstName = "Trader", LastName = "Demo", UserName = "trade@rzr.com", Email = "trade@rzr.com", PasswordHash = hasher.HashPassword("test123") }
);
context.SaveChanges();
}
}
}
&#13;
我对ApplicationUser
中的IdentityModels.cs
类进行了一些小的更改,我可以在修改此类时随时看到表架构更改(即我将自动迁移设置为真正)。但是,Seed()方法失败。
public class ApplicationUser : IdentityUser
{
// added Company, Name to profile - BM:
public string Company { get; set; }
public string FirstName { get; set; }
public string LastName{ get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
&#13;
仅供参考:在某些时候,当AppNetUsers
表为空时,种子数据成功。
然而,现在它完全不一致。我删除了AppNetUsers
中的记录,但Seed()方法仍然失败了。我不明白。
****更新**** 如果我在web.config文件中更改数据库名称,我可以强制它创建一个全新的数据库 - 而seed()数据工作正常!
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=MyDevBox;Initial Catalog=TestDB2;Integrated Security=True" providerName="System.Data.SqlClient" />
但是,当我尝试重新创建数据库时,我仍然不理解上面的Seed()方法中的异常?
谢谢你, 鲍勃答案 0 :(得分:2)
请勿将Identity对象直接添加到DbContext
使用角色管理器和用户管理器:
protected override void Seed(MarginWorkbenchNG.Models.ApplicationDbContext context)
{
var userManager = new ApplicationUserManager(new UserStore<Models.ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
if (!roleManager.RoleExists("SystemAdministrator"))
roleManager.Create(new IdentityRole("SystemAdministrator"));
var adminUser = userManager.FindByName("admin@rzr.com");
if (adminUser == null)
{
adminUser = new ApplicationUser
{
Company = "ICAP",
EmailConfirmed = true,
FirstName = "Admin",
LastName = "Demo",
UserName = "admin@rzr.com",
Email = "admin@rzr-risk.com"
};
userManager.Create(adminUser,"thePassword");
}
// adding roles to the user if necessary
if (!userManager.IsInRole(adminUser.Id, "SystemAdministrator"))
userManager.AddToRole(adminUser.Id, "SystemAdministrator");
}