简体   繁体   English

使用Entity Framework读取SQL Server表时无效的对象名称

[英]Invalid object name when using Entity Framework to read SQL Server table

I'm trying to set up a database connection using Entity Framework in an ASP.NET Core application, but each time I attempt to read from my data context, it fails stating that the table I'm trying to get is invalid. 我正在尝试使用ASP.NET核心应用程序中的Entity Framework建立数据库连接,但每次我尝试从我的数据上下文中读取时,它都会失败,说明我想要获取的表无效。

I'm relatively new to EF with ASP.NET Core, so I could be missing something obvious. 我对使用ASP.NET Core的EF相对较新,所以我可能会遗漏一些明显的东西。 How can I get EF to connect/find my object? 如何让EF连接/找到我的对象?

I have my connection string as follows: 我的连接字符串如下:

Data Source=DbServer;Initial Catalog=AIT;Integrated Security=True

And the table that I'm trying to read from is on DbServer : 我试图读取的表在DbServer

AIT.processing.orderProcessing

The error I'm getting when the DB context attempts to connect is this: 我在DB上下文尝试连接时得到的错误是:

SqlException: Invalid object name 'processing.orderProcessing'. SqlException:无效的对象名称'processing.orderProcessing'。

I've looked at the context itself when it runs, and it is connecting to the correct server. 我在运行时查看了上下文本身,并且它正在连接到正确的服务器。 I've also validated that the database is correct in the connection string and that it contains the processing.orderProcessing table. 我还验证了连接字符串中的数据库是正确的,并且它包含processing.orderProcessing表。 I've also tried to explicitly tell EF that it should be connecting to a specific object by using the Table property on my c# class: 我还尝试通过使用我的c#类上的Table属性明确告诉EF应该连接到特定对象:

[Table("AIT.processing.orderProcessing")]

This just causes my error message to change to: 这只会导致我的错误消息更改为:

SqlException: Invalid object name 'AIT.processing.orderProcessing'. SqlException:无效的对象名称'AIT.processing.orderProcessing'。

As far as I know, if it is connecting to DBServer , it should be able to find that table as it definitely exists, and it is spelled correctly. 据我所知,如果它连接到DBServer ,它应该能够找到该表,因为它肯定存在,并且拼写正确。 I just copy/pasted it from SSMS to make sure. 我只是从SSMS复制/粘贴它以确保。

Edit: The below answer was what fixed it for me. 编辑:以下答案是为我修复它的原因。 Changing my Table attribute to this: 将我的Table属性更改为:

[Table("orderProcessing", Schema = "processing")]

Caused EF to be able to connect. 导致EF能够连接。

So AIT is database name, processing is schema name and orderProcessing is the table name. 所以AIT是数据库名称, processing是模式名称, orderProcessing是表名称。

Try to set table name and scheme name separately using Data Annotations like: 尝试使用数据注释分别设置表名和方案名称,如:

[Table("orderProcessing", Schema = "processing")]

Or in DbContext OnModelCreating using: 或者在DbContext OnModelCreating使用:

 modelBuilder.Entity<MyEntity>().ToTable("orderProcessing", processing);

If your database name is "AIT" and schema is "processing" you must express EF core your schema name. 如果您的数据库名称为“AIT”且架构为“正在处理”,则必须将EF核心表示为您的架构名称。

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.HasDefaultSchema("processing");
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM