如何防止Entity Framework Core 2.0重命名生成的类中的表和列(数据库优先) [英] How to prevent Entity Framework Core 2.0 from Renaming Tables and Columns in Generated Classes (Database First)

查看:76
本文介绍了如何防止Entity Framework Core 2.0重命名生成的类中的表和列(数据库优先)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究需要利用实体框架的ASP.NET MVC Core 2.0项目(以.NET Core 2.0为目标).要使用的数据库已经存在,我无法更改它.我已将所有必需的引用添加到Entity Framework Core 2.0.

I have been working on an ASP.NET MVC Core 2.0 project (targeting .NET Core 2.0) that needs to make use of Entity Framework. The database to be used already exists and I can't change it. I have added all the required references to Entity Framework Core 2.0.

但是,当我从Nuget软件包管理器控制台运行以下脚手架命令时,我得到了与原始表相比大小写不同的表的POCO模型类.除其他差异外,名称中带有下划线的列由不带下划线的属性表示.

However, when I run the following scaffolding command from the Nuget Package Manager Console, I get POCO model classes for the tables with different capitalization as compared to the original tables. In addition to other differences, columns with underlines in their names are represented by properties without the underlines.

当生成POCO模型类时,如何强制脚手架保留所有表名和列名?

How can I force the scaffolding to leave all table and column names as is when the POCO model classes are generated?

脚手架命令示例.Scaffold-DbContext服务器=信息;数据库=车辆; Trusted_Connection =真实;"Microsoft.EntityFrameworkCore.SqlServer -OutputDir模型–force

Example of scaffold command. Scaffold-DbContext "Server= Info;Database=Vehicles;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models –force

(首次发布到Stack Overflow.感谢您的帮助.)

(First time posting to Stack Overflow. Thanks for your help.)

推荐答案

-UseDatabaseNames 选项用于保留与数据库中的名称完全相同的名称(如果不是,则仅进行较小的更正).t个有效的C#标识符).

The -UseDatabaseNames option is intended to preserve the names exactly as they are in the database (with only minor corrections if they aren't valid C# identifiers).

不幸的是,此选项在版本2.0中没有使用 Scaffold-DbContext 命令(问题#9146 ),并且它不适用于属性名称(问题#9820 ).您必须等待EF Core 2.1正常使用.如果您想尽快尝试,可以随时使用每晚的构建.

Unfortunately, this option got left off the Scaffold-DbContext command in version 2.0 (issue #9146) and it wasn't applied to property names (issue #9820). You'll have to wait for EF Core 2.1 to use it as it was intended. If you're eager to try it sooner, you can always use the nightly builds.

这篇关于如何防止Entity Framework Core 2.0重命名生成的类中的表和列(数据库优先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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