作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于某种原因,我需要在 EFCore 中使用 SQL,并且我将使用映射实体的表名。我怎样才能得到它?
最佳答案
使用Microsoft.EntityFrameworkCore.Relational 2.X 中的包:
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://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#provider ) 与 getter,因此您现在可以按如下方式访问架构:
var entityType = dbContext.Model.FindEntityType(typeof(YourEntity));
var schema = entityType.GetSchema();
var tableName = entityType.GetTableName();
关于asp.net-core - 如何在 Entity Framework Core 中获取映射实体的表名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45667126/
我是一名优秀的程序员,十分优秀!