Is any way to select a specific column from View with Entity Framework Core?

Viewed 24

I have a View with a lot of columns and a class mapped to this view

    public class MyClassConfig : IEntityTypeConfiguration<MyClass>
    {
        public void Configure(EntityTypeBuilder<MyClass> entity)
        {
            entity.ToTable(
                name: "MyClass_View",
                schema: DbContext.Schema);
        }
     }

In some queries, I do not need all the columns, and I want to select specific columns and that this would be done on the database side, like:

SELECT [v].[Prop1], [v].[Prop2]
FROM [schema].[MyClass_View] AS [v]

I create a query like this:

   var items = context.MyClassView.Where(x => x.Id == id)
              .Select(x => new
                           {
                                P1 = x.Prop1,
                                P2 = x.Prop2
                           });

But EF generates sql code with select all column from view. If I do the same query to the table this work perfectly. Is any way to do this for View?

0 Answers
Related