How to use views in db first entity framework

.net entity-framework entity-framework-core

Question

i have a question what i couldnt resolve it for 2 days. I'm working on .NET Core 2.2 by the way and thats why i'm using Entity Framework Core.

I have a views in my database and i dont know how to get view from code side. Because my views consisting of these;

CREATE VIEW table_column as
SELECT db.database_id,db.name,col.TABLE_NAME,col.COLUMN_NAME from sys.databases as db
INNER JOIN INFORMATION_SCHEMA.COLUMNS as col ON db.name = col.TABLE_CATALOG

Someone adviced me these;

var rawSQL = dbContext.Database.SqlQuery<SomeModel>("Raw SQL Query").ToList();

and this

var rawSQL = dbContext.SomeModels.FromSql("your SQL");

but they didnt work. As the last one i tried this;

how to use views in code first entity framework

but again didnt work because this is for the code first project.

AS A RESULT WHAT CAN I DO. I am about to go crazy.

1
0
6/14/2019 2:33:05 PM

Accepted Answer

The answer provided by Steve Greene is right.

Also In order to use views in EF Core you need to complete these points:

  1. Create entity to represent view result
  2. Create DbContext class
  3. Set mapping between entity and view (Data annotations of Fluent API)

Assuming you have setup those points, You'll perform a query like this:

var list = await dbContext.TableColumns.ToListAsync();

Let me know if this reply is useful.

1
6/14/2019 7:28:46 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow