How to Scaffold Controllers with database views to EF Core 2.1
Written on October 11, 2018
How to Scaffold Controllers with database views to EF Core 2.1
- Create view in database.
- Create a POCO with same structure as view.
- Add a new Controller with POCO created in step#2
a. If key related error occurrs, add a
Key
attribute on a column and then remove after scaffolding is completed. - A new property with
DbSet<T>
should have gotten added whereT
is the class created in step#2. ChangeDbSet
toDbQuery
. - In
OnModelCreating
method ofDbContext
, add following code:modelBuilder.Query<POCO from step#2>().ToView("Name of the view");