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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With