【问题标题】:Force Entity Framework to NOT use NCLOB datatype强制实体框架不使用 NCLOB 数据类型
【发布时间】:2017-07-23 07:54:39
【问题描述】:

所以我使用的是 ASP.NET Identity + Entity Framework,每当我从代码生成数据库时,很多列类型都是非常重的 NCLOB。

现在有几种方法可以修改列的类型,例如 HasColumnType 方法或通过设置 StringLength 属性或通过配置模型构建器为尚未拥有它的人提供 HasMaxLength 属性。

identity 的问题是一些专门用于 ID 的列设置得比较晚。

所以在迁移文件中我有这个:

                    UserId = c.String(nullable: false, maxLength: 128),
                    Email = c.String(maxLength: 256),
                    EmailConfirmed = c.Decimal(nullable: false, precision: 1, scale: 0),
                    PasswordHash = c.String(),
                    SecurityStamp = c.String(),
                    PhoneNumber = c.String(),

现在应用我在网上找到的这段代码后:

            modelBuilder.Properties()
          .Where(p => p.PropertyType == typeof(string) &&
                      p.GetCustomAttributes(typeof(MaxLengthAttribute), false).Length == 0 )
          .Configure(p => p.HasMaxLength(2000));

迁移代码变成这样:

                    UserId = c.String(nullable: false, maxLength: 2000),
                    Email = c.String(maxLength: 256),
                    EmailConfirmed = c.Decimal(nullable: false, precision: 1, scale: 0),
                    PasswordHash = c.String(maxLength: 2000),
                    SecurityStamp = c.String(maxLength: 2000),
                    PhoneNumber = c.String(maxLength: 2000),

如果您注意到了,UserID 已经设置为某个值,但即便如此,它还是发生了变化。

有没有办法让它不改变?

谢谢!!!

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework oracle11g asp.net-identity


    【解决方案1】:

    好吧,我现在只需要手动设置它们的属性,希望有一个更自动化的解决方案。

            modelBuilder.Entity<IdentityUser>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId").HasMaxLength(128);
            modelBuilder.Entity<ApplicationUser>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId").HasMaxLength(128);
            modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles").Property(p=> p.RoleId).HasMaxLength(128);
            modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles").Property(p => p.UserId).HasMaxLength(128);
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins").Property(p=> p.UserId).HasMaxLength(128);
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins").Property(p => p.ProviderKey).HasMaxLength(128);
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins").Property(p => p.LoginProvider).HasMaxLength(128);
            modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims").Property(p => p.UserId).HasMaxLength(128); ;
            modelBuilder.Entity<IdentityRole>().ToTable("Roles").Property(p => p.Id).HasMaxLength(128);
    

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 2017-12-24
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      相关资源
      最近更新 更多