diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 1f749bd830..6e87d3baa4 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -224,6 +224,21 @@ "source_path": "entity-framework/core/querying/overview.md", "redirect_url": "/ef/core/querying/how-query-works", "redirect_document_id": false + }, + { + "source_path": "entity-framework/core/modeling/included-types.md", + "redirect_url": "/ef/core/modeling/tables-and-types", + "redirect_document_id": false + }, + { + "source_path": "entity-framework/core/modeling/relational/tables.md", + "redirect_url": "/ef/core/modeling/tables-and-types", + "redirect_document_id": false + }, + { + "source_path": "entity-framework/core/modeling/relational/default-schema.md", + "redirect_url": "/ef/core/modeling/tables-and-types", + "redirect_document_id": false } ] } diff --git a/entity-framework/core/modeling/included-types.md b/entity-framework/core/modeling/included-types.md deleted file mode 100644 index 1b476f9331..0000000000 --- a/entity-framework/core/modeling/included-types.md +++ /dev/null @@ -1,36 +0,0 @@ ---- -title: Including & Excluding Types - EF Core -author: rowanmiller -ms.date: 10/27/2016 -ms.assetid: cbe6935e-2679-4b77-8914-a8d772240cf1 -uid: core/modeling/included-types ---- -# Including & Excluding Types - -Including a type in the model means that EF has metadata about that type and will attempt to read and write instances from/to the database. - -## Conventions - -By convention, types that are exposed in `DbSet` properties on your context are included in your model. In addition, types that are mentioned in the `OnModelCreating` method are also included. Finally, any types that are found by recursively exploring the navigation properties of discovered types are also included in the model. - -**For example, in the following code listing all three types are discovered:** - -* `Blog` because it is exposed in a `DbSet` property on the context - -* `Post` because it is discovered via the `Blog.Posts` navigation property - -* `AuditEntry` because it is mentioned in `OnModelCreating` - -[!code-csharp[Main](../../../samples/core/Modeling/Conventions/IncludedTypes.cs?name=IncludedTypes&highlight=3,7,16)] - -## Data Annotations - -You can use Data Annotations to exclude a type from the model. - -[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/IgnoreType.cs?highlight=20)] - -## Fluent API - -You can use the Fluent API to exclude a type from the model. - -[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/IgnoreType.cs?highlight=12)] diff --git a/entity-framework/core/modeling/relational/default-schema.md b/entity-framework/core/modeling/relational/default-schema.md deleted file mode 100644 index 8c25262de1..0000000000 --- a/entity-framework/core/modeling/relational/default-schema.md +++ /dev/null @@ -1,27 +0,0 @@ ---- -title: Default Schema - EF Core -author: rowanmiller -ms.date: 10/27/2016 -ms.assetid: e6e58473-9f5e-4a1f-ac0f-b87d2cbb667e -uid: core/modeling/relational/default-schema ---- -# Default Schema - -> [!NOTE] -> The configuration in this section is applicable to relational databases in general. The extension methods shown here will become available when you install a relational database provider (due to the shared *Microsoft.EntityFrameworkCore.Relational* package). - -The default schema is the database schema that objects will be created in if a schema is not explicitly configured for that object. - -## Conventions - -By convention, the database provider will choose the most appropriate default schema. For example, Microsoft SQL Server will use the `dbo` schema and SQLite will not use a schema (since schemas are not supported in SQLite). - -## Data Annotations - -You can not set the default schema using Data Annotations. - -## Fluent API - -You can use the Fluent API to specify a default schema. - -[!code-csharp[Main](../../../../samples/core/Modeling/FluentAPI/Relational/DefaultSchema.cs?name=DefaultSchema&highlight=7)] diff --git a/entity-framework/core/modeling/relational/tables.md b/entity-framework/core/modeling/relational/tables.md deleted file mode 100644 index b224016e55..0000000000 --- a/entity-framework/core/modeling/relational/tables.md +++ /dev/null @@ -1,72 +0,0 @@ ---- -title: Table Mapping - EF Core -author: rowanmiller -ms.date: 10/27/2016 -ms.assetid: c807aa4c-7845-443d-b8d0-bfc9b42691a3 -uid: core/modeling/relational/tables ---- -# Table Mapping - -> [!NOTE] -> The configuration in this section is applicable to relational databases in general. The extension methods shown here will become available when you install a relational database provider (due to the shared *Microsoft.EntityFrameworkCore.Relational* package). - -Table mapping identifies which table data should be queried from and saved to in the database. - -## Conventions - -By convention, each entity will be set up to map to a table with the same name as the `DbSet` property that exposes the entity on the derived context. If no `DbSet` is included for the given entity, the class name is used. - -## Data Annotations - -You can use Data Annotations to configure the table that a type maps to. - -``` csharp -using System.ComponentModel.DataAnnotations.Schema; - -[Table("blogs")] -public class Blog -{ - public int BlogId { get; set; } - public string Url { get; set; } -} -``` - -You can also specify a schema that the table belongs to. - -``` csharp -[Table("blogs", Schema = "blogging")] -public class Blog -{ - public int BlogId { get; set; } - public string Url { get; set; } -} -``` - -## Fluent API - -You can use the Fluent API to configure the table that a type maps to. - -``` csharp -using Microsoft.EntityFrameworkCore; - -class MyContext : DbContext -{ - public DbSet Blogs { get; set; } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity() - .ToTable("blogs"); - } -} - -public class Blog -{ - public int BlogId { get; set; } - public string Url { get; set; } -} -``` - -You can also specify a schema that the table belongs to. - -[!code-csharp[Main](../../../../samples/core/Modeling/FluentAPI/Relational/TableAndSchema.cs?name=Table&highlight=2)] diff --git a/entity-framework/core/modeling/types-and-tables.md b/entity-framework/core/modeling/types-and-tables.md new file mode 100644 index 0000000000..3ee12306b4 --- /dev/null +++ b/entity-framework/core/modeling/types-and-tables.md @@ -0,0 +1,77 @@ +--- +title: Types and Tables - EF Core +author: roji +ms.date: 12/03/2019 +ms.assetid: cbe6935e-2679-4b77-8914-a8d772240cf1 +uid: core/modeling/types-and-tables +--- +# Types and Tables + +Including 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 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 + +By convention, types that are exposed in DbSet properties on your context are included in the model as entities. 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` is included because it's exposed in a DbSet property on the context. +* `Post` is included because it's discovered via the `Blog.Posts` navigation property. +* `AuditEntry` because it is specified in `OnModelCreating`. + +[!code-csharp[Main](../../../samples/core/Modeling/Conventions/TypesAndTables.cs?highlight=3,7,16)] + +## Excluding types from the model + +If you don't want a type to be included in the model, you can exclude it: + +# [Data Annotations](#tab/data-annotations) + +[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/IgnoreType.cs?highlight=20)] + +# [Fluent API](#tab/fluent-api) + +[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/IgnoreType.cs?highlight=12)] + +*** + +> [!NOTE] +> The following sections are applicable only to relational databases such as SQL Server or Sqlite. + +## Table name + +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. If no DbSet exists for the given entity, the class name is used. + +You can manually configure the table name: + +# [Data Annotations](#tab/data-annotations) + +[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/Relational/Table.cs?highlight=11)] + +# [Fluent API](#tab/fluent-api) + +[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relational/Table.cs?highlight=11-12)] + +*** + +## Table schema + +When using a relational database, tables are by convention created in your database's default schema. 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: + +# [Data Annotations](#tab/data-annotations) + +[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relational/TableAndSchema.cs?highlight=2)] + +# [Fluent API](#tab/fluent-api) + +[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relational/TableAndSchema.cs?highlight=2)] + +*** + +Rather than specifying the schema for each table, you can also define the default schema at the model level with the fluent API: + +[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relational/DefaultSchema.cs?highlight=7)] + +Note that setting the default schema will also affect other database objects, such as sequences. diff --git a/entity-framework/toc.yml b/entity-framework/toc.yml index 2b57cb6879..28408f00e2 100644 --- a/entity-framework/toc.yml +++ b/entity-framework/toc.yml @@ -79,8 +79,8 @@ items: - name: Overview href: core/modeling/index.md - - name: "Including & Excluding Types" - href: core/modeling/included-types.md + - name: "Types & Tables" + href: core/modeling/types-and-tables.md - name: "Including & Excluding Properties" href: core/modeling/included-properties.md - name: Keys (primary) @@ -125,16 +125,12 @@ items: - name: Overview href: core/modeling/relational/index.md - - name: Table Mapping - href: core/modeling/relational/tables.md - name: Column Mapping href: core/modeling/relational/columns.md - name: Data Types href: core/modeling/relational/data-types.md - name: Primary Keys href: core/modeling/relational/primary-keys.md - - name: Default Schema - href: core/modeling/relational/default-schema.md - name: Computed Columns href: core/modeling/relational/computed-columns.md - name: Sequences diff --git a/samples/core/Modeling/Conventions/IncludedTypes.cs b/samples/core/Modeling/Conventions/TypesAndTables.cs similarity index 92% rename from samples/core/Modeling/Conventions/IncludedTypes.cs rename to samples/core/Modeling/Conventions/TypesAndTables.cs index 8df15809a0..ed3191bf2b 100644 --- a/samples/core/Modeling/Conventions/IncludedTypes.cs +++ b/samples/core/Modeling/Conventions/TypesAndTables.cs @@ -1,9 +1,9 @@ using Microsoft.EntityFrameworkCore; using System.Collections.Generic; -namespace EFModeling.Conventions.IncludedTypes +namespace EFModeling.Conventions.TypesAndTables { - #region IncludedTypes + #region TypesAndTables class MyContext : DbContext { public DbSet Blogs { get; set; }