ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 63.7k

which is better use data reader ado.net technology or entity framework

Oct 2 2023 10:54 PM

I work on blazor application server side I design web API to interact with razor pages

my issue is which is suitable for my scenario

using ado.net or entity framework core

what I try as below :

[Route("GetAllServersDetails")]
public IActionResult GetAllServersDetails()
{
    string query = "";
    query = "select s.ServerID, s.Server_Name as ServerName,isnull(s.ServerIp,'') as ServerIp,s.ServerTypeId,isnull(st.ServerType,'') as 
    ServerType,s.OsTypeId,isnull(os.DetailsName,'')  as OsType, s.HostedZoneId,isnull(hostedzone.DetailsName,'') as 
    HostedZone, s.ServerityId, isnull(serverity.DetailsName,'') as Serverity,s.HostedTypeId, isnull(hostedType.DetailsName,'') as HostedType, 
    isnull(s.ServerRoleId,0) as ServerRoleId,isnull(serverrole.DetailsName,'') as ServerRole,isnull(s.DRRequired,0) as 
    DRRequiredID,isnull(drrequired.DetailsName,'') as DRRequired,isnull(s.Remarks,'') as Remarks,isnull(s.OwnerFileNo,'') as ownerfilenumber, 
    s.IsActive from [dbo].[ServerNames] s with(nolock)
    inner join [dbo].[ServerTypes] st with(nolock) on st.ServerTypeId=s.ServerTypeId
    left join  Details os with(nolock) on os.ID=s.OsTypeId and os.HeaderId=12
    left join  Details hostedzone with(nolock) on hostedzone.ID=s.HostedZoneId and hostedzone.HeaderId=13
    left join  Details serverity with(nolock) on serverity.ID=s.ServerityId and serverity.HeaderId=14
    left join  Details hostedType with(nolock) on hostedType.ID=s.HostedTypeId and hostedType.HeaderId=15
    left join  Details serverrole with(nolock) on serverrole.ID=s.ServerRoleId and serverrole.HeaderId=16
    left join  Details drrequired with(nolock) on drrequired.ID=s.DRRequired and drrequired.HeaderId=17";
    try
    {
        ICollection<object> ApplicationsDataValues = new List<object>();
        using (var command = _context.Database.GetDbConnection().CreateCommand())
        {
            command.CommandText = query;
            command.CommandType = CommandType.Text;

            _context.Database.OpenConnection();

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    ApplicationsDataValues.Add(new
                    {
                        ServerID = reader.GetFieldValue<Int32>(0),
                        ServerName = reader.GetFieldValue<string>(1).ToString(),
                        ServerIp = reader.GetFieldValue<string>(2).ToString(),
                        ServerTypeId = reader.GetFieldValue<Int32>(3),
                        
                        ServerType = reader.GetFieldValue<string>(4).ToString(),
                        OsTypeId = reader.GetFieldValue<Int32>(5),
                        
                        OsType = reader.GetFieldValue<string>(6).ToString(),
                        HostedZoneId = reader.GetFieldValue<Int32>(7),
                        
                        HostedZone = reader.GetFieldValue<string>(8).ToString(),

                        ServerityId = reader.GetFieldValue<Int32>(9),
                        Serverity = reader.GetFieldValue<string>(10).ToString(),


                        HostedTypeId = reader.GetFieldValue<Int32>(11),
                        HostedType = reader.GetFieldValue<string>(12).ToString(),

                        ServerRoleId = reader.GetFieldValue<Int32>(13),
                        ServerRole = reader.GetFieldValue<string>(14).ToString(),

                        DRRequiredID = reader.GetFieldValue<string>(15).ToString(),
                        DRRequired = reader.GetFieldValue<string>(16).ToString(),
                        
                        Remarks = reader.GetFieldValue<string>(17).ToString(),
                        ownerfilenumber = reader.GetFieldValue<string>(18).ToString(),
                        
                        IsActive = reader.GetFieldValue<bool>(19)//.ToString()


                    });
                }
            }
        }
        return StatusCode(200, ApplicationsDataValues); // Get all users   
    }
    catch (Exception e)
    {
        return StatusCode(500, e);
    }
 
}

 

so are using data reader ado.net is best or using entity framework core

some developer tell me why use ado.net it is old technology and this is not good because it open and close connection with every load page and entity framework not do that

are this is correct

if that better or using entity framework core

I depend on code first technology

Result of statement above is 10000 rows as maximum


Answers (3)