gpt4 book ai didi

asp.net-core - 如何在 Entity Framework Core 中获取映射实体的表名称

转载 作者:行者123 更新时间:2023-12-01 23:39:35 25 4
gpt4 key购买 nike

出于某种原因,我需要在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com