How to Scaffold Controllers with database views to EF Core 2.1
Satish Yadav
1 min read
How to Scaffold Controllers with database views to EF Core 2.1
Create a view in the database.
Create a POCO with the same structure as the view.
Add a new Controller with POCO created in step#2 a. If key related error occurs, 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 the following code:
modelBuilder.Query<POCO from step#2>().ToView("Name of the view");
0
Subscribe to my newsletter
Read articles from Satish Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Satish Yadav
Satish Yadav
Manager — SDE