Skip to main content

Command Palette

Search for a command to run...

How to Scaffold Controllers with database views to EF Core 2.1

Updated
1 min read
How to Scaffold Controllers with database views to EF Core 2.1
S

Manager — SDE

How to Scaffold Controllers with database views to EF Core 2.1

  1. Create a view in the database.

  2. Create a POCO with the same structure as the view.

  3. 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.

  4. A new property with DbSet<T> should have gotten added where T is the class created in step#2. Change DbSet to DbQuery.

  5. In OnModelCreating method of DbContext, add the following code:

modelBuilder.Query<POCO from step#2>().ToView("Name of the view");

More from this blog

Satish’s Blog

32 posts