问题
EF Core 2.1 has new feature - Query Types.
Some of the main usage scenarios for query types are:
- Serving as the return type for ad hoc FromSql() queries.
- Mapping to database views.
- Mapping to tables that do not have a primary key defined.
- Mapping to queries defined in the model.
I upgrade project to Core 2.1, but Scaffold-DbContext still does not generate database views. I have to use a special parameter or the Scaffold-DbContext does not support it?
回答1:
Not supported in 2.1. See issue #1679.
回答2:
Here's a hackish but working solution:
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 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 following code:modelBuilder.Query<POCO from step#2>().ToView("Name of the view");
Source
来源:https://stackoverflow.com/questions/51378745/scaffold-dbcontext-for-database-views-in-ef-core-2-1-query-types