Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 如何在entity Framework Core中获取映射实体的表名_Asp.net Core_.net Core_Entity Framework Core - Fatal编程技术网

Asp.net core 如何在entity Framework Core中获取映射实体的表名

Asp.net core 如何在entity Framework Core中获取映射实体的表名,asp.net-core,.net-core,entity-framework-core,Asp.net Core,.net Core,Entity Framework Core,出于某种原因,我需要在EFCore中使用SQL,并且我将使用映射实体的表名。如何获取它?使用2.X中的包: var mapping = dbContext.Model.FindEntityType(typeof(YourEntity)).Relational(); var schema = mapping.Schema; var tableName = mapping.TableName; 这假设dbContext是从dbContext继承的类的实例,并且您已经在那里配置了YourEntity

出于某种原因,我需要在EFCore中使用SQL,并且我将使用映射实体的表名。如何获取它?

使用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()
提供程序扩展已被]()替换为getter,因此您现在可以按如下方式访问模式:

var entityType = dbContext.Model.FindEntityType(typeof(YourEntity));
var schema = entityType.GetSchema();
var tableName = entityType.GetTableName();

您可以使用这个静态类

公共静态类AttributeReader
{
//获取数据库表名
公共静态字符串GetTableName(DbContext上下文),其中T:class
{
//我们需要dbcontext来访问模型
var-models=context.Model;
//获取所有实体类型信息
var entityTypes=models.GetEntityTypes();
//T是类的名称
var entityTypeOfT=entityTypes.First(t=>t.ClrType==typeof(t));
var TableName annotation=entityTypeOfT.GetAnnotation(“Relational:TableName”);
var TableName=TableName注释.Value.ToString();
返回表名;
}
}
例如,我们有一个Person类,数据库中的实体名是People,我们可以从Person类中获取People

var-TblName=AttributeReader.GetTableName(YourContext);
EF Core 5版本 “现在允许实体类型同时映射到表和视图”

我不完全理解这一点,但这会增加查找表名的复杂性

对于我的用例,我实际上想要一个视图的名称,因此通过检查TableName和ViewName,如果提供的实体的类型错误,我可能会抛出一个错误

var entityType = typeof(Customer);
var modelEntityType = context.Model.FindEntityType(entityType);

string tableName = modelEntityType.GetSchemaQualifiedTableName();
string viewName = modelEntityType.GetSchemaQualifiedViewName();

if (tableName != null) 
{
   throw new Exception("The Entity " + entityName + " represents a table and not a view");
}

下面是西蒙的回答。在EF Core版本5x中,可以使用Microsoft扩展方法。创建了
DbContext
helper扩展方法:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

public static class EfCoreExtensions
{
   public static string GetSchemaQualifiedTableName(this DbContext context, Type entityType)
   {
      IEntityType et = context.Model.FindEntityType(entityType);
      //what to do here, entity could be both view and table!?
      //string viewName = et.GetSchemaQualifiedViewName();
      return et.GetSchemaQualifiedTableName();
   }
}
类似这样的使用示例:

  string tableName = _dbContext.GetSchemaQualifiedTableName(typeof(SomeEntity));
MS扩展方法位于类中:

Microsoft.EntityFrameworkCore.RelationalEntityTypeExtensions
引用使用



这似乎可行。这对获取所有表名有效吗<代码>var modelNames=context.Model.GetEntityTypes();foreach(modelNames中的var modelName){Console.WriteLine(modelName.Name);}此外,还有一个扩展方法可用于
IEntityType
。所以
entityType.GetTableName()
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.4" />