好!所以我扩展了我的IdentityUser,但它不再保存我的用户。代码示例可以在下面查看。我正试图用一个管理员用户为我的数据库播种,但它没有被存储。我试过在我的种子上运行一个调试器,但它不会触发任何东西。所以我有点迷路了。所以,这是代码。
我的扩展用户类别:
public class User : IdentityUser, IBaseEntity
{
public virtual ICollection<TimeLogEntry> TimeLogEntries { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public bool IsRemoved { get; set; }
public virtual User CreatedBy { get; set; }
public virtual User UpdatedBy { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager, string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}
然后当然是从IdentityDbContext:继承的上下文
public class Context : IdentityDbContext<User>, IContext
{
public Context()
: base("MyConnection", throwIfV1Schema: false)
{
Configuration.ProxyCreationEnabled = false;
}
// DBSETS
public static Context Create() => new Context();
// MODELBUILDER
}
最后是我的种子:
protected override void Seed(Context context)
{
// Launch debugger on seeds
//if (System.Diagnostics.Debugger.IsAttached == false)
// System.Diagnostics.Debugger.Launch();
SeedUsers(context);;
}
private void SeedUsers(Context context)
{
var manager = new UserManager<User>(new UserStore<User>(context));
var adminUser = new User
{
UserName = "Admin",
Email = "admin@test.com"
};
if (!manager.Users.Any())
{
manager.Create(adminUser, "Admin");
}
}
我希望有人能帮我!
初始值设定项中的Seed()仅在重新创建数据库时运行,例如在使用DropCreateDatabaseIfModelChanges时。
如果使用迁移(MigrateDatabaseToLatestVersion初始值设定项),则每次使用update-database
应用迁移时都会运行另一个Seed()。
http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/