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

查看:280
本文介绍了映射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天全站免登陆