Share via

ASP.NET Core Web App with SQL (No Entity Framework)

ChrisU 231 Reputation points
Dec 9, 2022, 2:51 PM

Hi All.

You will have to excuse me if this question is a bit confusing, but I have only been learning the ASP.NET MVC technology just this week.

I have used Flask and Django before and have this week been playing around with ASP.NET and have been following tutorials that setup an ASP.NET Core Web App. It all seems quite straight forward, and I am able to join the dots and I am sure I could get a competent looking website up and running in a few days.

However, as you will all know, Entity framework is a big part of this and features in pretty much every tutorial. This is fine; however, my place of work has asked me not to use Entity Framework and to use SQL as that is what the other apps here use and so you have a whole team very skilled in SQL. Therefore, they are keen for me to use a technology that the whole team know and can support. The use of Entity framework is a red line.

I like the MVC approach, and I have googled this over the past few days and there is not much around that I can find on directly using ASP.NET with SQL commands to get to and from the database. The only one I did find is this one: https://www.youtube.com/watch?v=T-e554Zt3n4&t=130s

My knowledge is limited, However in this tutorial the presenter did not seem use any of the MVC framework for the functionality, instead choosing to construct the functionality for all pages within the C# behind each page. I have followed this in a fashion but have implemented good practices like moving the DB connectivity to a shared function.

However, having learnt a bit more about ASP.NET CORE MVC In the meantime, I was wondering whether the MVC ASP.NET approach could be followed without using Entity framework? Is there any best/accepted practices or industry standards with this approach? Or is entity framework baked in? If so, what is the best approach to developing an ASP.NET web app when constrained to using SQL?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,788 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,490 questions
{count} vote

Accepted answer
  1. Zhi Lv - MSFT 32,986 Reputation points Microsoft Vendor
    Dec 13, 2022, 3:15 AM

    Hi @ChrisU ,

    I was wondering whether the MVC ASP.NET approach could be followed without using Entity framework?

    Yes, we can directly use ADO.Net to access the database without using Entity Framework(EF). Try to search "use ADO.Net in Asp.net core MVC", there should has multiple tutorials or samples. Such as below:

    CRUD Operations Using ASP.NET Core And ADO.NET

    Perform CRUD in Single View using ADO.Net in ASP.Net Core MVC

    Then the code like this:

    269797-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    3 people found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. simone cammarano 30 Reputation points
    Jan 9, 2024, 3:44 PM

    You could yous a micro-ORM like Dapper.

    User's image

    Dapper is know for its speed and simple implementation.

    You can download the NuGet Package and start to use it in your .Net

    This is the official link

    https://www.learndapper.com/

    0 comments No comments

  2. Lars Lawrenz 6 Reputation points
    Dec 9, 2022, 4:55 PM

    Hi Chris,

    yes many of the tutorials are done with Entity Framework. In ASP.Net or even ASP.Net Core you can use Microsoft Data SQLClient in combination with manually write the code to connect und query your database. From my perspective, I'm a developer with focus on database development, I suggest to use Stored Procedures to work with the database. From a performance an maintainability perspective it es the easiest way.

    It is always a good way not to use code behind. So your thought on this is right an you should follow that way.
    Entity Framework is not that much baked in that you are bound to use it to connect and work with a database. I faced several ways of handling connections to SQL Server through an application. The one side often uses OR-Mapper like Entity Framework with i.e. Code First approches. The other side used "direkt Access" to the database with SQL Client oder ODBC and writhing Stored Procedures or having queries direct in the code. There are some things in between, to be honest.

    This links should show you the basics working with SQL Server in .Net or .Net core. I hope they can give you an orientation of the way you can work with a SQL Database. I hope that it could help to get a start.

    Best regards,
    Lars

    1 person found this answer helpful.
    0 comments No comments

  3. Bruce (SqlWork.com) 71,696 Reputation points
    Dec 9, 2022, 9:01 PM

    as you may know entity framework is just an ORM. the examples use it because it simple and allows the tutorial to largely ignore sql syntax (also sqlite database is typically the database).

    you can use your own sql. the main design pattern is to define a sqlcontext for DI. I tend to use dapper as it maps to poco objects and is just extension methods to a sqlconnection.

        public class MyDb  
        {  
            private MyDbOptions _options { get; set; }  
            public MyDb(MyDbOptions options)  
            {  
                _options = options;  
            }  
      
            public async Task<SqlConnection> GetConnectionAsync()  
            {  
                var conn = new SqlConnection(_options.ConnectionString);  
                await conn.OpenAsync();  
                return conn;  
            }  
      
        }  
      
        public static class MyDbExtensions  
        {  
      
            public static async Task<MyDataModel> GetMyDataAsync(this MyDb db, string uuid)  
            {  
                using (var conn = await db.GetConnectionAsync())  
                {  
                    var jobs = await conn.QueryAsync<MyDataModel>("select * from dbo.mytable where uuid = @uuid", new { uuid });  
                    return jobs.First();  
                }  
            }  
        }  
    

    you can then register a MyDb for DI and use in mvc actions and libraries

    note: if you .net identity, its built with entity framework, and the

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.