简体   繁体   English

如何使用 EF.Core Database First 包含特殊字符 $ 脚手架表

[英]How do I Scaffold tables with EF.Core Database First containing special characters $

I have the following table in our SQL Server:我的 SQL Server 中有下表:

[prod].[dbo].[My Company$Customer]

I want to Scaffold this table with:我想用以下内容来搭建这张桌子:

Scaffold-DbContext "Server=SQLPROD;Database=prod;Trusted_Connection=True;integrated security=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -table "dbo.My Company$Customer"

But it will always give me the following error:但它总是会给我以下错误:

Unable to find a table in the database matching the selected table

If I omit the -table switch everything works fine, but I get ALL tables, which are A LOT and I don't need them.如果我省略 -table 开关,一切正常,但我得到所有表,这些表很多,我不需要它们。 So how can I use the -table switch correctly?那么如何正确使用 -table 开关呢? I am using ef.core 2.2.1我正在使用 ef.core 2.2.1

In SQL Server, if you have special characters in your table names (eg spaces or anything that isn't a letter or number) then you need to surround the object name with square brackets.在 SQL Server 中,如果您的表名中有特殊字符(例如空格或任何不是字母或数字的东西),那么您需要用方括号将对象名括起来。 So your command line would become something like this:所以你的命令行会变成这样:

Scaffold-DbContext "<connection-string>" 
    Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models 
    -table "[dbo].[My Company$Customer]"

You strictly don't need the square brackets around the schema ( dbo ) but there's no harm in including them there too.您绝对不需要模式( dbo )周围的方括号,但将它们包含在那里也没有坏处。

使用 EF Core 3.1 我不得不转义 $ 字符。

Scaffold-DbContext "Server=myServer;Database=myDB;User Id=sa;Password=xyz;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -table "[dbo].[My Company`$Product]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在LINQ或EF.Core中执行此操作? - How to do this in LINQ or EF.Core? 如何在 EF.core 中使用 FromSqlRaw 指定列 - How can I specific columns using FromSqlRaw in EF.core 使用 Code First 和 EF.Core 引用表以进行有效搜索的正确方法是什么 - What's the correct way to reference tables using Code First with EF.Core for searching efficiently 如何在 EF.Core 5 中添加具有相同外键的多个导航属性? - How can I add multiple navigation properties with the same Foreign Key in EF.Core 5? 如何让 EF Core 数据库首先使用 Enums? - How can I make EF Core database first use Enums? EF Core 数据库第一种方法:Scaffold-DbContext 不被识别为 cmdlet 的名称 - EF Core Database First Approach: Scaffold-DbContext is not recognized as the name of a cmdlet 数据库优先EF dnx ef dbcontext scaffold命令失败 - Database-first EF dnx ef dbcontext scaffold command fails .NET Core 3 / EF.Core 3:`QueryModelGenerator`、`RelationalQueryModelVisitor` 等发生了什么 - .NET Core 3 / EF.Core 3: what happened to `QueryModelGenerator`, `RelationalQueryModelVisitor` and etc 如何使用 EF Core 删除数据库中的多条记录? - How do I delete multiple records in database using EF Core? 如何将EF6与Database First和现有视图一起使用? - How do I use EF6 with Database First and existing views?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM