Asp.net-Core
如何在 Entity Framework Core 中獲取映射實體的表名
出於某種原因,我需要在 EFCore 中使用 SQL,並且我將使用映射實體的表名。我怎麼才能得到它?
使用2.X中的Microsoft.EntityFrameworkCore.Relational包:
var mapping = dbContext.Model.FindEntityType(typeof(YourEntity)).Relational(); var schema = mapping.Schema; var tableName = mapping.TableName;這假定它
dbContext是繼承自DbContext並且您已YourEntity在那裡配置的類的實例。請注意,在 EF Core 3.X 中,[
.Relational()提供程序擴展已被替換](https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/break-changes #provider ) 與 getter,因此您現在可以按如下方式訪問模式:var entityType = dbContext.Model.FindEntityType(typeof(YourEntity)); var schema = entityType.GetSchema(); var tableName = entityType.GetTableName();