【问题标题】:Remove column from AspNetUsers从 AspNetUsers 中删除列
【发布时间】:2017-03-16 23:20:19
【问题描述】:

我创建了一个数据库上下文。我想删除表AspNetUsers 中的一些列,例如PhoneNumber, PhoneNumberConfirmed 因为在我的应用程序中没有必要。

namespace TestTest.Infrastuctures
{
    public class TestContext:IdentityDbContext<ApplicationUser>
    {
        public TestContext()
            : base("TestContext")
        {

        }
        public static TestContext Create()
        {
            return new TestContext();
        }
        public virtual DbSet<Category> Categories { get; set; }
        public virtual DbSet<Product> Products { get; set; }

    }

    public class ApplicationUserEntityTypeConfiguration : EntityTypeConfiguration<ApplicationUser>
    {
        public ApplicationUserEntityTypeConfiguration()
        {
            Ignore(p => p.PhoneNumber);
            Ignore(p => p.PhoneNumberConfirmed);
            Ignore(p => p.EmailConfirmed);
            Ignore(p => p.TwoFactorEnabled);

        }
    }
}

代码工作正常,但没有删除 AspNetUsers 中的选定列。

欢迎任何帮助或建议。

更新

这段代码解决了我的问题:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            var user = modelBuilder.Entity<ApplicationUser>();
            user.Ignore(u => u.EmailConfirmed);
            user.Ignore(u => u.PhoneNumber);
            user.Ignore(u => u.PhoneNumberConfirmed);

            var identityUserRole = modelBuilder.Entity<IdentityUserRole>();
            identityUserRole.HasKey(r => new { r.UserId, r.RoleId });

            var identityUserLogin = modelBuilder.Entity<IdentityUserLogin>();
            identityUserLogin.HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId });

            var claims = modelBuilder.Entity<IdentityUserClaim>();

       }

【问题讨论】:

  • 你的更新基本上就是我的回答。

标签: asp.net-mvc entity-framework


【解决方案1】:

我相信您可以做到这一点,但不一定使用EntityTypeConfiguration 方法。而是尝试在您的上下文中覆盖 OnModelCreating 方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<IdentityUser>()
        .Ignore(p => p.PhoneNumber)
        .Ignore(p => p.PhoneNumberConfirmed)
        .Ignore(p => p.EmailConfirmed)
        .Ignore(p => p.TwoFactorEnabled);    
}

【讨论】:

    【解决方案2】:

    您不能删除任何内置属性。他们在那里支持身份功能。无论您是否真的需要电子邮件确认,了解电子邮件是否已被确认都很有价值。

    添加额外的属性就像任何其他实体一样。创建一个继承自 IdentityUser 的类(如果还没有的话),然后添加您喜欢的任何属性。

    【讨论】:

      【解决方案3】:

      进入ApplicationUser类,删除PhoneNumber、PhoneNumberConfirmed的属性。

      添加迁移,然后更新您的数据库。

      【讨论】:

      • 这些属性来自 Identity 框架内的一个类,如果不从 Githugb 下载整个内容并自己编译,则无法编辑。
      猜你喜欢
      • 2014-10-22
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多