How to get Table Name of mapped entity in Entity F

2019-02-11 14:04发布

问题:

In some reason, I need to use SQL in EFCore, and I will use table name of mapped entity. How can I get it?

回答1:

Using the Microsoft.EntityFrameworkCore.Relational package:

var mapping = dbContext.Model.FindEntityType(typeof(YourEntity)).Relational();
var schema = mapping.Schema;
var tableName = mapping.TableName;

This assumes that dbContext is a instance of class that inherits from DbContext and that you have YourEntity configured there.