Skip to content

Commit

Permalink
Add documentation for view mapping.
Browse files Browse the repository at this point in the history
Fixes #480
Fixes #2138
  • Loading branch information
AndriySvyryd committed Oct 6, 2020
1 parent 3f2f4cc commit e9e4413
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
11 changes: 11 additions & 0 deletions entity-framework/core/modeling/entity-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ Rather than specifying the schema for each table, you can also define the defaul
[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/DefaultSchema.cs?name=DefaultSchema&highlight=3)]

Note that setting the default schema will also affect other database objects, such as sequences.

## View mapping

Entity types can be mapped to database views using the Fluent API.

> [!Note]
> EF will assume that the referenced view already exists in the database, it will not create it automaticall in a migration.
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/ViewNameAndSchema.cs?name=ViewNameAndSchema&highlight=1)]

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.
2 changes: 1 addition & 1 deletion entity-framework/core/modeling/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ CREATE TABLE [PostTag] (

Internally, EF creates an entity type to represent the join table that will be referred to as the join entity type. There is no specific CLR type that can be used for this, so `Dictionary<string, object>` is used. More than one many-to-many relationships can exist in the model, therefore the join entity type must be given a unique name, in this case `PostTag`. The feature that allows this is called shared-type entity type.

The many to many navigations are called skip navigations as they effectively skip over the join entity type. If you are employing bulk configuration all skip navigations can be obtained from `GetSkipNavigations`.
The many to many navigations are called skip navigations as they effectively skip over the join entity type. If you are employing bulk configuration all skip navigations can be obtained from <xref:Microsoft.EntityFrameworkCore.Metadata.IMutableEntityType.GetSkipNavigations%2A>.

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relationships/ManyToManyShared.cs?name=Metadata)]

Expand Down
23 changes: 23 additions & 0 deletions samples/core/Modeling/FluentAPI/ViewNameAndSchema.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;

namespace EFModeling.FluentAPI.Relational.ViewNameAndSchema
{
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region ViewNameAndSchema
modelBuilder.Entity<Blog>()
.ToView("blogsView", schema: "blogging");
#endregion
}
}

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

0 comments on commit e9e4413

Please sign in to comment.