Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core 3.1 to 5 Update -> IEntityType.GetTableName

In EF Core 3.1 this was working

// Model
builder.Entity<MyEntity>().HasNoKey().ToView("MyView", "dbo");

// Then I have this Function
public static GetTableName<T>(this MyDbContext db)
{
      var t = db.Model.FindEntityType(typeof(T));
      return t.GetTableName()
}

In EF Core 5 t.GetTableName() returns null because the method supposed to be used is t.GetViewName()

so I changed my code to

return t.GetTableName() ?? t.GetViewName()

However I think its "ugly" and I would rather check if t is a view or a table and then call the right t.Get[View/Tabe]Name. But I didn't find any property or method on t itself to find out what it is.

Any Ideas and how to find out if it's a view or a table?

like image 444
gsharp Avatar asked Nov 23 '25 11:11

gsharp


1 Answers

Even Entity Framework Core source is using the GetViewName() method to figure out if an object is a view.

With that in mind, I think your solution is concise and you could make your own extension method that does exactly what you have there to hide the "uglyness":

public static string GetTableOrViewName(this IEntityType entityType) =>
    entityType.GetTableName() ?? entityType.GetViewName();
like image 90
AndreasHassing Avatar answered Nov 25 '25 23:11

AndreasHassing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!