-1

First of all, I have seen this post: Raw SQL Query without DbSet - Entity Framework Core but I feel like it does not answer my question.

I have an ASP.NET Core MVC project with Entity Framework Core (newest at this date).

What I need to do is:

public IActionResult MyView(string NameOfTable)
{
    // send model that is same as NameOfTable 
    // or
    // send something like List<string> that has properties and names of columns  
    return View();
}

What I want to do is call a function and send the name of a table in database. It will find the table in the database and return the properties of table (column names, types of columns[int/ varchar..]).

What I think will work: either write raw SQL to database.

What I think is good answer: some snippet of how to write raw SQL to query the database without knowing a model. Or some way of overcoming this problem.

Thanks for everything. Sorry if this is dumb question.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrew Black
  • 129
  • 1
  • 11
  • Yeah sorry, I am very sleepy as its 2 in morning at my timezone and was really annoyed by this coplication in my code. I am posting solution. – Andrew Black Jun 13 '20 at 23:44
  • 1
    I am confused why you are using entity framework, but not actually using it really. Seems kinda weird, but I am sure its for a reason. Why not just use DbSet, create migration, apply it, and then use them in a context like EF wants you too? – mathis1337 Jun 13 '20 at 23:47
  • I am using it properly in my whole solution so I want it to be something like project wide and I dont want to download more libraries for one controller – Andrew Black Jun 13 '20 at 23:49

1 Answers1

2

Sorry for bothering you all. I have found solution. Here it is:

public class AA
{
    public string One { get; set; }
    public string Two { get; set; }
}

private async Task pokus()
{
    List<AA> groups = new List<AA>();
    var conn = _db.Database.GetDbConnection();
    try
    {
        await conn.OpenAsync();
        using (var command = conn.CreateCommand())
        {
            string query = "SELECT * FROM TABLE";
            command.CommandText = query;
            DbDataReader reader = await command.ExecuteReaderAsync();

            if (reader.HasRows)
            {
                while (await reader.ReadAsync())
                {
                    try
                    {
                        var row = new AA { One = reader.GetString(1), Two = reader.GetString(2) };
                        groups.Add(row);
                    }
                    catch { }
                }
            }
            else
            {
                Console.WriteLine("Dont have rows");
            }
            reader.Dispose();
        }
    }
    finally
    {
        conn.Close();
    }
    foreach(AA s in groups)
    {
        Console.WriteLine(s.One);
        Console.WriteLine(s.Two);
    }
}

I hope it will help somebody.

PoLáKoSz
  • 355
  • 1
  • 6
  • 7
Andrew Black
  • 129
  • 1
  • 11