向 DbContext 添加新实体
Add new entities to DbContext
我使用 ASP.NET Core with Identity 并希望扩展默认的 Db 上下文。如果我想添加未链接的 table 我只需添加一个新的 class:
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);
});
}
}
然后创建迁移和更新数据库。有用。但是如果我想添加一个新的 table,它从 IdentityDbContext 链接到 table:
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 class 不存在(据我了解,它是由 IdentityDbContext 创建的)。如何正确操作?
class 很可能被命名为 ApplicationUser
(默认值)。代表这个实体的table是dbo.AspNetUsers
,但那是由Identity设定的,与class名字无关。
FWIW,但是,创建一个 Users
实体是个坏主意,原因有很多:
肯定会混淆Users
和ApplicationUser
,还有数据库tables dbo.Users
和dbo.AspNetUsers
.
一般来说,您应该以单数时态命名您的实体,即 User
,而不是 Users
。这个约定有很多原因,但我只想说,它只是让你的代码更好,更具可读性,坚持单数事物的单数时态和复数事物的复数时态。例如,ICollection<User>
类型的 属性 将被命名为 Users
,因为它由许多 User
个实例组成。
你的所作所为完全没有必要。 Identity 存在的全部原因是 Membership(ASP.NET 使用的以前的身份验证和授权框架)不允许您扩展所涉及的类型。 Identity 改变了这一切,并且在各个方面都是 100% 可扩展的。您可以完全访问框架中涉及的所有实体,并且可以添加到它们并从中派生。如果您想在系统中为 "users" 添加其他属性,只需将它们直接添加到 ApplicationUser
class。
我使用 ASP.NET Core with Identity 并希望扩展默认的 Db 上下文。如果我想添加未链接的 table 我只需添加一个新的 class:
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);
});
}
}
然后创建迁移和更新数据库。有用。但是如果我想添加一个新的 table,它从 IdentityDbContext 链接到 table:
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 class 不存在(据我了解,它是由 IdentityDbContext 创建的)。如何正确操作?
class 很可能被命名为 ApplicationUser
(默认值)。代表这个实体的table是dbo.AspNetUsers
,但那是由Identity设定的,与class名字无关。
FWIW,但是,创建一个 Users
实体是个坏主意,原因有很多:
肯定会混淆
Users
和ApplicationUser
,还有数据库tablesdbo.Users
和dbo.AspNetUsers
.一般来说,您应该以单数时态命名您的实体,即
User
,而不是Users
。这个约定有很多原因,但我只想说,它只是让你的代码更好,更具可读性,坚持单数事物的单数时态和复数事物的复数时态。例如,ICollection<User>
类型的 属性 将被命名为Users
,因为它由许多User
个实例组成。你的所作所为完全没有必要。 Identity 存在的全部原因是 Membership(ASP.NET 使用的以前的身份验证和授权框架)不允许您扩展所涉及的类型。 Identity 改变了这一切,并且在各个方面都是 100% 可扩展的。您可以完全访问框架中涉及的所有实体,并且可以添加到它们并从中派生。如果您想在系统中为 "users" 添加其他属性,只需将它们直接添加到
ApplicationUser
class。