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.

2
  • 1
    I would remove the EF 6 tag if this refers to EF core. If it is EF core, views are discussed here Commented Jun 14, 2019 at 14:20
  • As linked in Steves Comment, please look here: msdn.microsoft.com/magazine/mt847184 Commented Aug 20, 2019 at 11:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much your advice gave me some idea and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.