diff --git a/entity-framework/core/modeling/included-types.md b/entity-framework/core/modeling/included-types.md deleted file mode 100644 index c54035b2f1..0000000000 --- a/entity-framework/core/modeling/included-types.md +++ /dev/null @@ -1,71 +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` - - -``` csharp -class MyContext : DbContext -{ - public DbSet Blogs { get; set; } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity(); - } -} - -public class Blog -{ - public int BlogId { get; set; } - public string Url { get; set; } - - public List 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; } -} -``` - -## Data Annotations - -You can use Data Annotations to exclude a type from the model. - -[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/Samples/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/Samples/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 900d269851..0000000000 --- a/entity-framework/core/modeling/relational/default-schema.md +++ /dev/null @@ -1,38 +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. - - -``` csharp -class MyContext : DbContext -{ - public DbSet Blogs { get; set; } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.HasDefaultSchema("blogging"); - } -} -``` diff --git a/entity-framework/core/modeling/relational/tables.md b/entity-framework/core/modeling/relational/tables.md deleted file mode 100644 index 8ecf90e07f..0000000000 --- a/entity-framework/core/modeling/relational/tables.md +++ /dev/null @@ -1,78 +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; -``` -``` csharp -[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; -``` -``` csharp -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. - - -``` csharp - modelBuilder.Entity() - .ToTable("blogs", schema: "blogging"); -``` 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..2767bbf8d2 --- /dev/null +++ b/entity-framework/core/modeling/types-and-tables.md @@ -0,0 +1,160 @@ +--- +title: Types and Tables - EF Core +author: rowanmiller +ms.date: 10/27/2016 +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's model; we usually refer to such a type as an *entity*. EF can read and write entity instances from/to the database, and if you're using a relational database, EF can create tables for your entities via migrations. + +## Conventions + +By convention, types that are exposed in DbSet properties on your context are included in your model as entities. 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 types. When using a relational database, each type will be mapped to a table with the same name as the DbSet property that exposes the entity; if no DbSet is included for the given entity, the class name is used. + +In the code sample below, all types are included and their corresponding database tables are named as follows: + +* `Blog` is included because it's exposed in a DbSet property on the context. Its database table takes its name from the DbSet property, `Blogs`. +* `Post` is included because it's discovered via the `Blog.Posts` navigation property. As it has no DbSet property, its table will be named `Post`, based on its type. +* `AuditEntry` because it is mentioned in `OnModelCreating`. Like `Post`, its table is named based on its type, `AuditEntry`. + + +``` csharp +class MyContext : DbContext +{ + public DbSet Blogs { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity(); + } +} + +public class Blog +{ + public int BlogId { get; set; } + public string Url { get; set; } + + public List 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: + +# [Data Annotations](#tab/data-annotations) + + +``` csharp +[NotMapped] +public class BlogMetadata +{ + public DateTime LoadedFromDatabase { get; set; } +} +``` + +# [Fluent API](#tab/fluent-api) + + +``` csharp +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder.Ignore(); +} +``` + +*** + +## Configuring the table name + +When using a relational database, you can override the table name configured by convention: + +# [Data Annotations](#tab/data-annotations) + + +``` csharp +using System.ComponentModel.DataAnnotations.Schema; + +[Table("blogs")] +public class Blog +{ + public int BlogId { get; set; } + public string Url { get; set; } +} +``` + +# [Fluent API](#tab/fluent-api) + + +``` csharp +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder.Entity() + .ToTable("blogs"); +} +``` + +*** + +## Configuring the table schema + +When using a relational database, tables are by default created in your database's 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). + +You can configure tables to be created in a specific schema as follows: + +# [Data Annotations](#tab/data-annotations) + + +``` csharp +using System.ComponentModel.DataAnnotations.Schema; + +[Table("blogs", Schema = "blogging")] +public class Blog +{ + public int BlogId { get; set; } + public string Url { get; set; } +} +``` + +# [Fluent API](#tab/fluent-api) + + +``` csharp +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder.Entity() + .ToTable("blogs", schema: "blogging"); +} +``` + +*** + +To avoid specifying the schema for every table, you can also define the default schema at the model level with the fluent API: + + +``` csharp +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder.HasDefaultSchema("blogging"); +} +``` + +Note that setting the default schema will also affect other database tables, e.g. sequences. diff --git a/entity-framework/toc.md b/entity-framework/toc.md index 2775867e16..fd55502a12 100644 --- a/entity-framework/toc.md +++ b/entity-framework/toc.md @@ -50,7 +50,7 @@ #### [Configuring a DbContext](core/miscellaneous/configuring-dbcontext.md) ### [Creating a Model](core/modeling/index.md) -#### [Including & Excluding Types](core/modeling/included-types.md) +#### [Types and Tables](core/modeling/types-and-tables.md) #### [Including & Excluding Properties](core/modeling/included-properties.md) #### [Keys (primary)](core/modeling/keys.md) #### [Generated Values](core/modeling/generated-properties.md) @@ -72,11 +72,9 @@ #### [Alternating models with same DbContext](core/modeling/dynamic-model.md) #### [Spatial Data (GIS)](core/modeling/spatial.md) #### [Relational Database Modeling](core/modeling/relational/index.md) -##### [Table Mapping](core/modeling/relational/tables.md) ##### [Column Mapping](core/modeling/relational/columns.md) ##### [Data Types](core/modeling/relational/data-types.md) ##### [Primary Keys](core/modeling/relational/primary-keys.md) -##### [Default Schema](core/modeling/relational/default-schema.md) ##### [Computed Columns](core/modeling/relational/computed-columns.md) ##### [Sequences](core/modeling/relational/sequences.md) ##### [Default Values](core/modeling/relational/default-values.md) diff --git a/samples/core/Modeling/Conventions/Samples/IncludedTypes.cs b/samples/core/Modeling/Conventions/Samples/TypesAndTables.cs similarity index 93% rename from samples/core/Modeling/Conventions/Samples/IncludedTypes.cs rename to samples/core/Modeling/Conventions/Samples/TypesAndTables.cs index aa6015e71d..999c17669c 100644 --- a/samples/core/Modeling/Conventions/Samples/IncludedTypes.cs +++ b/samples/core/Modeling/Conventions/Samples/TypesAndTables.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; using System.Collections.Generic; -namespace EFModeling.Conventions.Samples.IncludedTypes +namespace EFModeling.Conventions.Samples.TypesAndTables { class MyContext : DbContext {