映射 api 中的 EF Core 列名称 [英] EF Core column name from mapping api

查看:27
本文介绍了映射 api 中的 EF Core 列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 EF 6.1 中引入了映射 API,我们最终可以在其中访问表名和列名.在 EF Core 中获取表名是一个非常好的变化,但我还没有发现如何获取列名.

In EF 6.1 the mapping API was introduced where we could finally get access to table names and column names. Getting the table name is a very nice change in EF Core, but I have not yet uncovered how to get the column names.

对于任何对此感兴趣的人来说,我是如何在最新版本 (RC1) 中获取表名的

For anyone interested here is how I got the table name in the latest version (RC1)

context.Model.FindEntityType(typeof(T)).SqlServer().TableName

当前获取列名的方法是什么,还是尚不可用?

What is the current method to get column names or is this not available yet?

推荐答案

var columnNames = ctx.Model.FindEntityType(typeof (T))
                           .GetProperties().Select(x => x.SqlServer().ColumnName)
                           .ToList();

还有

var columnNames = ctx.Model.FindEntityType(typeof (T))
                           .GetProperties().Select(x => x.Relational().ColumnName)
                           .ToList();

在 EF Core 3.X 中,.Relational().SqlServer() 已被替换,你可以简单地使用:

In EF Core 3.X, .Relational() and .SqlServer() have been replaced and you can simply use:

var columnNames = ctx.Model.FindEntityType(typeof (T))
                           .GetProperties().Select(x => x.GetColumnName())
                           .ToList();

这篇关于映射 api 中的 EF Core 列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆