2017-01-03 32 views
9

Tôi đã bắt đầu với NET Core, trong MVC 5 Tôi đã thay đổi tên bảng mặc định ví dụ: AspNETUsers để người dùng theo cách này và nó đã làm việc một cách hoàn hảo: Trong IdentityModels Class I dded:Làm cách nào để thay đổi tên bảng ASP.NET Identity mặc định trong .NET CORE?

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

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

Nhưng nó không hoạt động trong .NET CORE (MVC 6). Có ai có thể giúp tôi không? Cảm ơn rất nhiều.

+1

Xin đừng sử dụng: NET Core như đồng nghĩa với ASP.NET Lõi! .NET Core là một thời gian chạy, như thời gian chạy .NET 4.x. ASP.NET Core ở phía bên kia là một framework/webstack và chạy trên ** BOTH **, .NET Core và .NET> = 4.5.1 – Tseng

+0

@Tseng, tôi đang học Core, chắc chắn có nhiều điểm khác biệt giữa ASP.NET và Core. –

Trả lời

7

Cố gắng thay đổi ràng buộc để

builder.Entity<ApplicationUser>(entity => 
     { 
      entity.ToTable(name:"Users"); 
      entity.Property(e => e.Id).HasColumnName("UserId"); 

     }); 
5

-Để thay đổi tên của những bảng (IdentityUserRole <Tkey>, IdentityUserToken <Tkey>, IdentityRoleClaim <Tkey>, IdentityUserClaim <Tkey>, IdentityUserLogin <Tkey>), bạn phải xác định loại TKey (The loại được sử dụng cho khóa chính) là chuỗi (GUID) theo mặc định ngay cả khi bạn không thay đổi nó.

-Nếu bạn muốn thay đổi khóa chính từ GUID để int https://medium.com/@goodealsnow/asp-net-core-identity-3-0-6018fc151b4

protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 

     builder.Entity<ApplicationUser>(entity => 
     { 
      entity.ToTable(name: "User");   
     }); 

     builder.Entity<IdentityRole>(entity => 
     { 
      entity.ToTable(name: "Role"); 
     }); 
     builder.Entity<IdentityUserRole<string>>(entity => 
     { 
      entity.ToTable("UserRoles"); 
      //in case you chagned the TKey type 
      // entity.HasKey(key => new { key.UserId, key.RoleId }); 
     }); 

     builder.Entity<IdentityUserClaim<string>>(entity => 
     { 
      entity.ToTable("UserClaims"); 
     }); 

     builder.Entity<IdentityUserLogin<string>>(entity => 
     { 
      entity.ToTable("UserLogins"); 
      //in case you chagned the TKey type 
      // entity.HasKey(key => new { key.ProviderKey, key.LoginProvider });  
}); 

     builder.Entity<IdentityRoleClaim<string>>(entity => 
     { 
      entity.ToTable("RoleClaims"); 

     }); 

     builder.Entity<IdentityUserToken<string>>(entity => 
     { 
      entity.ToTable("UserTokens"); 
      //in case you chagned the TKey type 
      // entity.HasKey(key => new { key.UserId, key.LoginProvider, key.Name }); 

     }); 
    } 
+0

Các câu trả lời chỉ có mã được khuyến khích trên SO. Bạn có thể sử dụng một vài từ và giải thích cách giải quyết vấn đề này không? – ImportanceOfBeingErnest

+0

Tôi đã thêm mã ở trên vào ứng dụng mvc core asp.net 2.0 của mình. Làm sạch và xây dựng lại dự án và xóa thư mục Migrations, sau đó chạy cập nhật cơ sở dữ liệu trong giao diện điều khiển nuget và nó vẫn tạo ra các bảng nhận dạng bằng cách sử dụng các tên AspNet * gốc. – alexb

+0

Xin chào Alexb, tôi đã tạo .NET Core 2.0 mới và tôi đã sử dụng cùng một mã để thay đổi các tên bảng định danh ASP.NET mặc định và nó hoạt động tốt với tôi. Bạn đã kiểm tra ApplicationDbContextModelSnapshot để xem liệu di chuyển của bạn có thành công với các tên bảng mới hay không? –

Các vấn đề liên quan