如何在Entity Framework Core中获取映射实体的表名

时间:2017-08-14 03:41:21

标签: asp.net-core .net-core entity-framework-core

在某些原因,我需要在EFCore中使用SQL,我将使用映射实体的表名。我怎么能得到它?

4 个答案:

答案 0 :(得分:34)

使用Microsoft.EntityFrameworkCore.Relational包:

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->helper(array('url', 'form'));
    $this->load->model("usermodel");
    $this->load->library('session');
}

private function view($page, $data=false) {
  if($page == "auth/login" ){
        $this->load->view("auth/header_auth.php");
    }else{
        $this->load->view("header.php");
  }

  $this->load->view($page, $data);
  $this->load->view("footer.php");
}

public function index() {
  if ($this->session->userdata("user")) {
      redirect("dashboard", "refresh");
      return;
  }
  $this->view("auth/login");
}

public function fail() {
  $this->view("auth/login");

}

public function dashboard() {
  $this->view("auth/dashboard");
}

public function login() {
  $login = $this->input->post("login");
  $password = $this->input->post("password");
  if ($this->usermodel->login($login, $password)) {
      $this->session->set_userdata("user", $login);
      redirect("dashboard", "refresh");
  } else {
      redirect("fail", "refresh");
  }
}

public function logout() {
  $this->session->unset_userdata('user');
  session_destroy();
  redirect('index', 'refresh');
}

}

这假设var mapping = dbContext.Model.FindEntityType(typeof(YourEntity)).Relational(); var schema = mapping.Schema; var tableName = mapping.TableName; 是继承自dbContext的类的实例,并且您在那里配置了DbContext

答案 1 :(得分:1)

您可以使用此静态类

public static class AttributeReader
{
    //Get DB Table Name
    public static string GetTableName<T>(DbContext context) where T : class
    {
        // We need dbcontext to access the models
        var models = context.Model;

        // Get all the entity types information
        var entityTypes = models.GetEntityTypes();

        // T is Name of class
        var entityTypeOfT = entityTypes.First(t => t.ClrType == typeof(T));

        var tableNameAnnotation = entityTypeOfT.GetAnnotation("Relational:TableName");
        var TableName = tableNameAnnotation.Value.ToString();
        return TableName;
    }

}

例如,我们有一个Person类,数据库中的实体名称为People,我们可以从person类中获取人。

var TblName= AttributeReader.GetTableName<(YourModel)>(YourContext);

答案 2 :(得分:0)

EF Core 5 版本

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");
}

答案 3 :(得分:0)

继西蒙的回答之后。在 EF Core 版本 5x 中,可以使用 Microsoft 扩展方法。创建了一个 DbContext 辅助扩展方法:

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

使用 Nuget 引用

<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.4" />