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 6622d0b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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 automatically in a migration.
[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/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.
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 6622d0b

Please sign in to comment.