0

I can get Table name from Entity/Class name but I need to get Class name from Table name.

[Table("MyAccount")]
public class Account
{ public string abc {get; set;}}

I want to get Account class from controller using "MyAccount" string.

I want to get the class details like class name using table name parameter and DbContext. I don't want to access table.

deloyar
  • 438
  • 6
  • 9

1 Answers1

0

I am not sure that I understand your question. Normally you define a DbSet<Account> in your DbContext and use this set to access the table. No need to pass the table name anywhere!

public class MyDbContext : DbContext {
    public IDbSet<Account> Accounts { get; set; }
}

using (var context = new MyDbContext()) { 
    var theAccount = context.Accounts.Find(accountId);
    var abc = theAccount.abc;
}

If you need dynamic access, use the DbContext.Set method.

Type accountType = typeof(Account);
var allAccounts = context.Set(accountType).ToArray();

See Entity Framework Working with DbContext.

This answer shows how to get the DbSet by table name: Dynamically access table in EF Core 2.0

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • I want to get the class details like class name using table name parameter and DbContext. I don't want to access table. – deloyar May 29 '18 at 08:38