How to get column name and corresponding database type from DbContext in Entity Framework Core

Suppose I have this table:

enter image description here

How can I get the column name and database data type from DbContextin the Entity Framework core?

Advice

  • A column named clg # converted to clg1 using the EF Core Scaffold tool, so I need the name of the real column, not the current name of EF

  • I need a database type, not clrType, of course, there must be a cross platform. Perhaps I will modify the database so that this method works too.

Desired Result:

    <D.clg#, int>
    <D.clgname, nvarchar(50)>
    <D.city, nvarchar(50)>
    <D.pname, nvarchar(50)>

Can someone provide a solution?

+5
source share
1 answer

(EF Core 3.x): EF Core 3.0, API - Relational() , Get Set, :

var entityType = dbContext.Model.FindEntityType(clrEntityType);

// Table info 
var tableName = entityType.GetTableName();
var tableSchema = entityType.GetSchema();

// Column info 
foreach (var property in entityType.GetProperties())
{
    var columnName = property.GetColumnName();
    var columnType = property.GetColumnType();
};

(EF Core 2.x): EF Core 2.0, , . EF Core , Relational():

var entityType = dbContext.Model.FindEntityType(clrEntityType);

// Table info 
var tableName = entityType.Relational().TableName;
var tableSchema = entityType.Relational().Schema;

// Column info 
foreach (var property in entityType.GetProperties())
{
    var columnName = property.Relational().ColumnName;
    var columnType = property.Relational().ColumnType;
};

(EF Core 1.x):

EF Core , EF - DbContext.Model, IModel, GetEntityTypes FindEntityType, IEntityType, GetProperties FindProperty, IProperty ..

, EF Core . , , , IRelationalDatabaseProviderServices AnnotationProvider TypeMapper , .

:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Storage;

public class DbColumnInfo
{
    public string Name;
    public string Type;
}

public static class RelationalDbHelpers
{
    public static IEnumerable<DbColumnInfo> GetDbColums(this DbContext dbContext, Type clrEntityType)
    {
        var dbServices = dbContext.GetService<IDbContextServices>();
        var relationalDbServices = dbServices.DatabaseProviderServices as IRelationalDatabaseProviderServices;
        var annotationProvider = relationalDbServices.AnnotationProvider;
        var typeMapper = relationalDbServices.TypeMapper;

        var entityType = dbContext.Model.FindEntityType(clrEntityType);

        // Not needed here, just an example 
        var tableMap = annotationProvider.For(entityType);
        var tableName = tableMap.TableName;
        var tableSchema = tableMap.Schema;

        return from property in entityType.GetProperties()
               let columnMap = annotationProvider.For(property)
               let columnTypeMap = typeMapper.FindMapping(property)
               select new DbColumnInfo
               {
                   Name = columnMap.ColumnName,
                   Type = columnTypeMap.StoreType
               };
    }
}
+20

Source: https://habr.com/ru/post/1670818/


All Articles