1

因此,我使用ASP.NET Identity +實體框架,每當我從代碼生成數據庫時,許多列類型都是非常重的NCLOB。強制實體框架不使用NCLOB數據類型

現在有幾種方法來修改類型的列的如HasColumnType方法,或通過設置StringLength屬性或通過配置模型構建器給予HasMaxLength attribue對於那些還沒有的話誰。

身份問題在於某些專門用於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已被設置爲某個值,但即使如此沒有改變。

有沒有解決方法,它不會改變?

謝謝!

回答

0

嗯,我只是不得不手動設置他們的屬性,現在希望更自動的解決方案。

 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);