The Fluent API ToView Method

The ToView method is applied to a type to specify the name of the database view or table that the type should map to. The ToView method is mainly used for mapping query types to tables or views.

The following example specifies that the OrderHeader query type should map to a database view named vw_OrderHeaders:

public class SampleContext : DbContext
{
    public DbQuery<OrderHeader> OrderHeaders { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Query<OrderHeader>()
            .ToView("vw_OrderHeaders");
    }
}

public class OrderHeader
{
    public string CustomerName { get; set; }
    public DateTime DateCreated { get; set; }
    public int TotalItems { get; set; }
    public decimal TotalPrice { get; set; }
}

An overload of the method maps the entity to a non-default schema:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Query<OrderHeader>()
            .ToView("vw_OrderHeaders", "Accounts");
}

Data Annotations

There is no Data Annotation equivalent to the ToView method.

Last updated: 15/08/2018 06:43:35

© 2018 - Learn Entity Framework Core.
All rights reserved.