Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge relational and regular table docs #1717

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/entity-types",
"redirect_document_id": false
},
{
"source_path": "entity-framework/core/modeling/relational/tables.md",
"redirect_url": "/ef/core/modeling/entity-types",
"redirect_document_id": false
},
{
"source_path": "entity-framework/core/modeling/relational/default-schema.md",
"redirect_url": "/ef/core/modeling/entity-types",
"redirect_document_id": false
}
]
}
75 changes: 75 additions & 0 deletions entity-framework/core/modeling/entity-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: Entity Types - EF Core
description: How to configure and map entity types using Entity Framework Core
author: roji
roji marked this conversation as resolved.
Show resolved Hide resolved
ms.date: 12/03/2019
ms.assetid: cbe6935e-2679-4b77-8914-a8d772240cf1
uid: core/modeling/entity-types
---
# Entity Types

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 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/EntityTypes.cs?name=EntityTypes&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)]

***

## 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/DataAnnotations/Relational/TableAndSchema.cs?name=Table&highlight=1)]

### [Fluent API](#tab/fluent-api)

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relational/TableAndSchema.cs?name=Table&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?name=DefaultSchema&highlight=7)]

Note that setting the default schema will also affect other database objects, such as sequences.
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.

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: Entity Types
href: core/modeling/entity-types.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.EntityTypes
{
#region IncludedTypes
#region EntityTypes
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ class MyContext : DbContext
public DbSet<Blog> Blogs { get; set; }
}

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