0

I am learning ASP.NET Core and Razor Pages. In my practice project, I am trying to execute a select query on a database which I have added through migrations (code-first).

Here is the code:

public string DataTest()
{
    var data = CheckDbEntryAsync();
    var d1 = data.ToString();
    return data.ToString();
}

public async Task<List<UserData>> CheckDbEntryAsync()
{
   List<UserData> data;

   data = await _context.UserDataItems.FromSql("SELECT * from dbo.UserDataItems").ToListAsync();

   return data;
}

The output (the value of 'data.ToString()' or variable 'd1') is:

"System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Collections.Generic.List`1[LearnASP.Models.UserData],LearnASP.Controllers.UserManagerController+<CheckDbEntryAsync>d__6]"

I can't figure out how to make sense of this...shouldn't this be a list that contains values from the table?

The value of 'data' before the CheckDbEntryAsync is executed is:

Id = 242, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

Here is my UserDataDbContext.cs:

public class UserDataDbContext : DbContext
{
    public DbSet<UserData> UserDataItems { get; set; }

    public UserDataDbContext(DbContextOptions<UserDataDbContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

I can't figure out where exactly I am going wrong...is it the query which is not executing...or it is something else...there are no error messages.

Please help, Thanks

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
D Navi
  • 11
  • 3
  • ASP.NET is a web framework, not a data access library or ORM. It doesn't access databases. What you posted here shows Entity Framework Core contexts. – Panagiotis Kanavos Apr 17 '19 at 07:12
  • The problem itself though isn't related to either. You can't just call `ToString()` on a List and get its data. Unless a type overloads the `ToString()` method to return something meaningful, you'll get the `Object.ToString()` implementation that just prints the object's type. In this case though, you called `ToString()` on a *task*, not a list. `data` in `DataTest` is a `Task>` – Panagiotis Kanavos Apr 17 '19 at 07:16
  • Possible duplicate of [How does the .ToString() method work?](https://stackoverflow.com/questions/10075751/how-does-the-tostring-method-work) – Panagiotis Kanavos Apr 17 '19 at 07:17
  • Thanks. So if I understood correctly, I have to find a way to extract data from Task> ?? – D Navi Apr 17 '19 at 07:36
  • You already use that - you already call `await` in other parts of the code. And you still need to properly format the results into a string, you can't just call `ToString()` on a list. The duplicate explains why – Panagiotis Kanavos Apr 17 '19 at 07:41
  • BTW what's the point of using `_context.UserDataItems.FromSql("SELECT * from dbo.UserDataItems").ToListAsync()`? It's no different than just calling : `_context.UserDataItems.ToListAsync()`. EF, like all ORMs, converts query operations to SQL itself. Without any `Where()` or `Select()` operators it will just load the entire table – Panagiotis Kanavos Apr 17 '19 at 07:44
  • Actually that is not the exact query that I am going to run. I had put some qualifiers earlier...but I couldn't understand what was going on and so I used ToString and the Select * query to simplify things a bit. I am fairly new to C# and ASP.NET. With your help I am getting there...and will post it as an answer. – D Navi Apr 17 '19 at 09:50

0 Answers0