asp.net-mvc – 实体框架种子与身份(Microsoft.Owin.Security)用
我有一个为数据库设置种子的类,它为2个用户添加了角色和自定义字段.我遇到的问题是它将数据保存在[dbo].[AspNetUsers]而不是[dbo].[IdentityUsers].两个表都已创建.播种时,数据进入AspNetUser.当我启动网站并注册新用户时,数据将进入IdentityUser.
这是Migration类: internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext> { public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(DatabaseContext context) { base.Seed(context); var userStore = new UserStore<ApplicationUser>(); var manager = new UserManager<ApplicationUser>(userStore); var role = new IdentityUserRole { Role = new IdentityRole(Model.Roles.ADMINISTRATOR) }; var user = new ApplicationUser() { UserName = "123123",Email = "123123@123.com",Language = "en-US"}; user.Roles.Add(role); IdentityResult result = manager.Create(user,"123123"); var role2 = new IdentityUserRole { Role = new IdentityRole(Model.Roles.NORMAL) }; var user2 = new ApplicationUser() { UserName = "qweqwe",Email = "qweqwe@qweqwe.com",Language = "fr-CA" }; user.Roles.Add(role2); IdentityResult result2 = manager.Create(user2,"qweqwe"); } } 以下是ApplicationUser类,它定义Identity模型的自定义字段. public class ApplicationUser : IdentityUser,ICurrentUser { public ApplicationUser() { Email = ""; Language = ""; } public string UserId { get { return base.Id; } set{} } public string Email { get; set; } public string Language { get; set; } } 以下是此新类的Entity Framework配置类. public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser> { public ApplicationUserConfiguration() { this.HasKey(d => d.Id); this.Ignore(d => d.UserId); } } 两者都使用具有相同配置的save DataContext: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //... others entity here modelBuilder.Configurations.Add(new ApplicationUserConfiguration()); //Theses configuration are required since custom fields are added to ApplicationUser. Here is why : https://stackoverflow.com/questions/19474662/map-tables-using-fluent-api-in-asp-net-mvc5-ef6 modelBuilder.Entity<IdentityUserLogin>().HasKey(l => l.UserId); modelBuilder.Entity<IdentityRole>().HasKey(r => r.Id); modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId,r.UserId }); } 我的问题是:为什么我有从Identity表复制的AspNet前缀表名?为什么种子在Web应用程序使用另一种时使用一种? 解决方法
您的DbContext是否继承自IdentityDbContext?
如果从IdentityDbContext继承,则不需要在OnModelCreating中进行任何其他配置. IdentityDbContext提供的映射和默认的实体框架约定应该足够了. 如果在没有UserId的setter的情况下声明ApplicationUser,则不需要忽略UserId属性: public class ApplicationUser : IdentityUser { public ApplicationUser() { Email = ""; Language = ""; } public string Email { get; set; } public string Language { get; set; } public string UserId { get { return Id; } } } 然后你的ApplicationDbContext可以这么简单(为了清楚起见,我已将Configuration类重命名为MigrationConfiguration): public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { static ApplicationDbContext() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext,MigrationConfiguration>()); } public ApplicationDbContext() : base("DefaultConnection") { } } 这与AspNet.Identity.Core和AspNet.Identity.EntityFramework程序集的发行版本(版本1.0.0)一样正常工作(只有名为AspNetXXXX的表,AspNetUsers表具有自定义字段). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.net Web窗体,在编译时获取aspx /视图错误?
- asp.net – 要排除的Nuget和源控制文件?
- asp.net – 在使用母版页时,如何为标记分配特定的名称?
- ASP.NET单元测试 – WatiN和Windows 7 / Internet Explorer
- 对ASP.Net组件套件有什么好评
- asp.net-mvc – ASP.NET MVC将表单值传递给action方法
- asp.net – 无法将参数值从字符串转换为guid
- ASP.NET MVC:从FileResult返回大量数据
- asp.net-mvc – 无法加载文件或程序集Microsoft.VisualStud
- asp.net – 确定当前页面是否需要授权?
- ASP基于B/S模式的失物招领系统
- asp.net-mvc – asp.net mvc通用控制器
- asp.net-mvc – 数据库中已经有一个名为“AspNet
- asp.net-mvc-3 – MVC3:用于发送表单(模型)值和
- asp.net – MVC 5 Web API与Facebook访问令牌到R
- model-view-controller – 将模型转换为viewmode
- ASP.NET Web.config转换将无法在安装程序中使用
- ASP.NET会员密码到期
- asp.net-mvc – 如何从ActionExecuting过滤器中获
- asp.net-mvc – ASP.net MVC中的流畅验证 – 数据