實體類型Entity Types

在您的內容中包含類型的 DbSet,表示它包含在 EF Core 的模型中;我們通常會將這類型別稱為 實體Including a DbSet of a type on your context means that it is included in EF Core's model; we usually refer to such a type as an entity. EF Core 可以從資料庫讀取和寫入實體實例,如果您使用關係資料庫,EF Core 可以透過遷移來為您的實體建立資料表。EF Core can read and write entity instances from/to the database, and if you're using a relational database, EF Core can create tables for your entities via migrations.

在模型中包含類型Including types in the model

依照慣例,在內容中公開于 DbSet 屬性中的型別會以實體的形式包含在模型中。By convention, types that are exposed in DbSet properties on your context are included in the model as entities. 也會包含方法中指定的實體類型 OnModelCreating ,如同以遞迴方式探索其他探索到之實體類型的導覽屬性所找到的任何類型。Entity types that are specified in the OnModelCreating method are also included, as are any types that are found by recursively exploring the navigation properties of other discovered entity types.

下列程式碼範例中包含所有類型:In the code sample below, all types are included:

  • Blog 包含在內容上的 DbSet 屬性中,因此會包含在內。Blog is included because it's exposed in a DbSet property on the context.
  • Post 包含,因為它是透過 Blog.Posts 導覽屬性來探索。Post is included because it's discovered via the Blog.Posts navigation property.
  • AuditEntry 因為它是在中指定的 OnModelCreatingAuditEntry because it is specified in OnModelCreating.
class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AuditEntry>();
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public Blog Blog { get; set; }
}

public class AuditEntry
{
    public int AuditEntryId { get; set; }
    public string Username { get; set; }
    public string Action { get; set; }
}

從模型排除類型Excluding types from the model

如果您不想要將類型包含在模型中,您可以將它排除:If you don't want a type to be included in the model, you can exclude it:

[NotMapped]
public class BlogMetadata
{
    public DateTime LoadedFromDatabase { get; set; }
}

從遷移中排除Excluding from migrations

注意

在 EF Core 5.0 中新增了從遷移中排除資料表的功能。The ability to exclude tables from migrations was added in EF Core 5.0.

將相同的實體類型對應到多個型別時,有時會很有用 DbContextIt is sometimes useful to have the same entity type mapped in multiple DbContext types. 尤其是 在使用系結內容時更是如此,因為每個限定的內容通常會有不同的 DbContext 類型。This is especially true when using bounded contexts, for which it is common to have a different DbContext type for each bounded context.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>()
        .ToTable("blogs", t => t.ExcludeFromMigrations());
}

使用此設定時,將不會建立 blogs 資料表,但 Blog 仍會包含在模型中,而且可以正常使用。With this configuration migrations will not create the blogs table, but Blog is still included in the model and can be used normally.

如果您需要再次使用遷移來開始管理資料表,則應該在未排除的位置建立新的遷移 blogsIf you need to start managing the table using migrations again then a new migration should be created where blogs is not excluded. 下一個遷移現在會包含對資料表所做的任何變更。The next migration will now contain any changes made to the table.

資料表名稱Table name

依照慣例,每個實體類型都會設定成對應至資料庫資料表,名稱與公開實體的 DbSet 屬性相同。By convention, each entity type will be set up to map to a database table with the same name as the DbSet property that exposes the entity. 如果指定的實體沒有 DbSet 存在,則會使用類別名稱。If no DbSet exists for the given entity, the class name is used.

您可以手動設定資料表名稱:You can manually configure the table name:

[Table("blogs")]
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

資料表結構描述Table schema

使用關係資料庫時,資料表會依照慣例建立在您資料庫的預設架構中。When using a relational database, tables are by convention created in your database's default schema. 例如,Microsoft SQL Server 會使用 dbo 架構 (SQLite 不支援架構) 。For example, Microsoft SQL Server will use the dbo schema (SQLite does not support schemas).

您可以設定要在特定架構中建立的資料表,如下所示:You can configure tables to be created in a specific schema as follows:

[Table("blogs", Schema = "blogging")]
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

您也可以使用流暢的 API,在模型層級定義預設架構,而不是指定每個資料表的架構:Rather than specifying the schema for each table, you can also define the default schema at the model level with the fluent API:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema("blogging");
}

請注意,設定預設架構也會影響其他資料庫物件,例如序列。Note that setting the default schema will also affect other database objects, such as sequences.

視圖對應View mapping

您可以使用流暢的 API,將實體類型對應至資料庫檢視。Entity types can be mapped to database views using the Fluent API.

注意

EF 會假設參考的視圖已經存在於資料庫中,而不會在遷移時自動建立。EF will assume that the referenced view already exists in the database, it will not create it automatically in a migration.

modelBuilder.Entity<Blog>()
    .ToView("blogsView", schema: "blogging");

對應至 view 會移除預設的資料表對應,但是實體類型也可以明確地對應到資料表。Mapping to a view will remove the default table mapping, but the entity type can also be mapped to a table explicitly. 在此情況下,查詢對應將用於查詢,而資料表對應將用於更新。In this case the query mapping will be used for queries and the table mapping will be used for updates.