0

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.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • 1
    I would remove the EF 6 tag if this refers to EF core. If it is EF core, views are discussed [here](https://stackoverflow.com/questions/36012616/working-with-sql-views-in-entity-framework-core) – Steve Greene Jun 14 '19 at 14:20
  • As linked in Steves Comment, please look here: https://msdn.microsoft.com/magazine/mt847184 – Roman R. Aug 20 '19 at 11:00

1 Answers1

1

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.

H. Herzl
  • 3,030
  • 1
  • 14
  • 19