Skip to content

Commit

Permalink
Merge relational and regular table docs
Browse files Browse the repository at this point in the history
* Merged modeling/relational/tables and modeling/relational/default-schema
  into modeling/included-types and renamed to types-and-tables.
* Show DataAnnotation/FluentAPI as tabs

Part of #1669
  • Loading branch information
roji committed Dec 3, 2019
1 parent bb0abf8 commit ba46584
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 143 deletions.
15 changes: 15 additions & 0 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
36 changes: 0 additions & 36 deletions entity-framework/core/modeling/included-types.md

This file was deleted.

27 changes: 0 additions & 27 deletions entity-framework/core/modeling/relational/default-schema.md

This file was deleted.

72 changes: 0 additions & 72 deletions entity-framework/core/modeling/relational/tables.md

This file was deleted.

77 changes: 77 additions & 0 deletions entity-framework/core/modeling/types-and-tables.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 2 additions & 6 deletions entity-framework/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Blog> Blogs { get; set; }
Expand Down

0 comments on commit ba46584

Please sign in to comment.