In my Seed() method of Configuration.cs, I'm getting some errors when adding data:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
StackTrace
at System.Data.Entity.Internal.InternalContext.SaveChanges()
My Configuration.cs file with seed data:
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();
}
}
}
I have made a couple of minor changes to my ApplicationUser
class in IdentityModels.cs
, and I can certainly see the table schema changes any time I modify this class (i.e. I have auto-migration set to true). However, Seed() method fails.
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;
}
}
FYI: At some point, when the AppNetUsers
table was empty, the seed data was successful.
However, it's totally inconsistent now. I've deleted the records in AppNetUsers
, and yet the Seed() method is stil failing. I don't understand.
**** UPDATE **** If I change the DB name in my web.config file, I can force it to create a brand new DB - and the seed() data works fine !
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=MyDevBox;Initial Catalog=TestDB2;Integrated Security=True" providerName="System.Data.SqlClient" />
However, I still don't understand the exceptions in my Seed() method above when I try to RE-CREATE the DB ?
thank you, Bob