我使用帶有標識的ASP.NET Core並希望擴展默認的Db上下文。如果我想補充不掛表我只需添加一個新的類:向DbContext添加新實體
public partial class Table1
{
public int Id { get; set; }
public string Txt { get; set; }
}
並致以ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public virtual DbSet<Table1> Table1 { get; set; }
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<Table1>(entity =>
{
entity.ToTable("Table_1");
entity.Property(e => e.Id).HasColumnName("ID");
entity.Property(e => e.Txt)
.IsRequired()
.HasMaxLength(50);
});
}
}
然後創建一個遷移和更新數據庫。有用。但是,如果我想添加一個新的表,它鏈接到表從IdentityDbContext:
public partial class Users
{
public int Id { get; set; }
public string UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual AspNetUser User { get; set; }
}
當然
,AspNetUser類不存在(它是由IdentityDbContext創建的,據我所知)。如何正確地做到這一點?
我必須添加它,因爲它已經完成項目,基於會員供應商 –
身份和會員是互相排斥的。無論是升級到身份或堅持會員。你絕對不應該試圖同時使用兩者。 –