上方設置 public class ApplicationDbContext : IdentityDbContext
如果錯誤=>設置:using Microsoft.AspNetCore.Identity;
新增Model獲取
public DbSet ApplicationUsers { get; set; }
*資料庫中AspNetUsers資料表已經定義好USER基礎的欄位。
*可以新增自定義的屬性:
在Models資料夾底下新增自定義的ApplicationUser繼承IdentityUser
增加Name與Address欄位。
*更新資料庫:
Add-migration ExtendIdntityUser
Update-database
public class ApplicationUser : IdentityUser
{
[Required]
public string Name { get; set; }
public string Address { get; set; }
}
複製Category的Repository與IRepository,
再用ApplicationUser取代Category。
只保留下列程式即可。
public interface IApplicationUserRepository : IRepository<ApplicationUser>
{
}
public class ApplicationUserRepository : Repository<ApplicationUser>, IApplicationUserRepository
{
private ApplicationDbContext _db;
public ApplicationUserRepository(ApplicationDbContext db) : base(db)
{
_db = db;
}
}
開啟UnitOfWork與IUnitOfWork做以下修改
UnitOfWork:
public IApplicationUserRepository ApplicationUser { get; private set; }
ApplicationUser = new ApplicationUserRepository(_db);
IUnitOfWork:
IApplicationUserRepository ApplicationUser { get; }
1.資料表會多一個Discriminator欄位,用於紀錄IdentityUser,
2.開啟Areas\Identity\Pages\Account\Register.cshtml.cs更新為ApplicationUser