ASP.NET core - can I use an sql view instead of a table in my app?

Michael Penney 41 Reputation points
2020-05-27T16:14:37.853+00:00

Hi,

I was just wondering if anyone could offer a bit of help. I'm currently a work term student and I've been asked to create a view in SQL server and use that for the application we are developing. Creating a view is easy enough but I'm not sure on how to use the view inside of the application. I'm building the app with asp.net core. Any help would be appreciated!

Thanks,
Mike P.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,916 questions
{count} votes

Accepted answer
  1. Mark Allan 421 Reputation points MVP
    2020-05-27T17:23:26.887+00:00

    Assuming you're using Entity Framework Core, make sure you're using EF Core 3 and then it will scaffold views for you as keyless entity types (previously known as query types)- see https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/#reverse-engineering-of-database-views. If you're tied to a previous version, you can still do it with query types in 2.1 as long as you scaffold it yourself in OnModelCreating, or whatever version you're on you can always fall back to a raw SQL SELECT statement.

    At a lower level, if you're not using EF then for most purposes you can treat views as read-only tables.

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Manoj Sharma 1 Reputation point
    2020-05-27T17:29:45.637+00:00

    Hi Mike,

    If you are use Views in the same manner as you would use a SQL Server database table object.

    So, you can either use a stored procedure to query the view and than invoke Stored procedure using ADO.NET from your ASP.NET Core application or you can also use a select query with ADO.NET against you view.

    You can refer to sample code of using ADO.NET here (https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ado-net-code-examples)

    -Thanks,
    Manoj

    0 comments No comments

  2. Виктор C 1 Reputation point
    2020-06-06T20:14:23.713+00:00
    0 comments No comments

  3. Dave Patrick 426.1K Reputation points MVP
    2020-06-06T21:05:10.863+00:00

    Asp.net core is not currently supported here on QnA. I'd try asking for help in dedicated forums here.

    https://forums.asp.net/1255.aspx/1?ASP+NET+Core

    https://stackoverflow.com/questions/tagged/asp.net-core

    --please don't forget to Accept as answer if the reply is helpful--


    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    0 comments No comments