简体   繁体   中英

Entity Framework throws exception - Invalid object name 'dbo.BaseCs'

I've followed Adam's answer here and the Entity Framework now works and the Seed() method also works.

But when I try to access the database like this:

    public User FindUserByID(int id)
    {
        return (from item in this.Users
                where item.ID == id
                select item).SingleOrDefault();
    }
  .............................................................................
    // GET: /Main/

    public ActionResult Index(int? id)
    {
        var db = UserDataBaseDB.Create();

        if (!id.HasValue)
            id = 0;

        return View(db.FindUserByID(id.Value));
    }

It throws an exception at return (from item in this.Users stating:

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.BaseCs'.

I've tried replacing it with: return this.Users.ElementAt(id); but then it throws this exception.

LINQ to Entities does not recognize the method 'MySiteCreator.Models.User ElementAt[User](System.Linq.IQueryable 1[MySiteCreator.Models.User], Int32)' method, and this method cannot be translated into a store expression.`

Can anyone help me?
Thank you!

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.BaseCs'

This error means that EF is translating your LINQ into a sql statement that uses an object (most likely a table) named dbo.BaseCs , which does not exist in the database.

Check your database and verify whether that table exists, or that you should be using a different table name. Also, if you could post a link to the tutorial you are following, it would help to follow along with what you are doing.

It is most likely a mismatch between the model class name and the table name as mentioned by 'adrift'. Make these the same or use the example below for when you want to keep the model class name different from the table name (that I did for OAuthMembership). Note that the model class name is OAuthMembership whereas the table name is webpages_OAuthMembership.

Either provide a table attribute to the Model:

[Table("webpages_OAuthMembership")]
public class OAuthMembership

OR provide the mapping by overriding DBContext OnModelCreating:

class webpages_OAuthMembershipEntities : DbContext
{
    protected override void OnModelCreating( DbModelBuilder modelBuilder )
    {
        var config = modelBuilder.Entity<OAuthMembership>();
        config.ToTable( "webpages_OAuthMembership" );            
    }
    public DbSet<OAuthMembership> OAuthMemberships { get; set; }        
}

If you are providing mappings like this:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ClassificationMap());
        modelBuilder.Configurations.Add(new CompanyMap());
        modelBuilder.Configurations.Add(new GroupMap());
        ....  
    }

Remember to add the map for BaseCs.

You won't get a compile error if it is missing. But you will get a runtime error when you use the entity.

It might be an issue about pluralizing of table names. You can turn off this convention using the snippet below.

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     base.OnModelCreating(modelBuilder);
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
 }

If everything is fine with your ConnectionString check your DbSet collection name in you db context file. If that and database table names aren't matching you will also get this error.

So, for example, Categories, Products

public class ProductContext : DbContext 
{ 
    public DbSet<Category> Categories { get; set; } 
    public DbSet<Product> Products { get; set; } 
}

should match with actual database table names:

在此处输入图像描述

EF is looking for a table named dbo.BaseCs. Might be an entity name pluralizing issue. Check out this link .

EDIT: Updated link.

我的修复就像确保正确的连接字符串在所有 appsettings.json 文件中一样简单,而不仅仅是默认的。

Instead of

modelBuilder.Entity<BaseCs>().ToTable("dbo.BaseCs");

Try:

modelBuilder.Entity<BaseCs>().ToTable("BaseCs");

even if your table name is dbo.BaseCs

I don't know if is the case,

If you create a migration before adding a DbSet your sql table will have a name of your model, generally in singular form or by convention we name DbSet using plural form.

So try to verifiy if your DbSet name have a same name as your Table. If not try to alter configuration.

Most probably the translated SQL statement can't find the table name.

In my case, the table was assigned to a different schema. So, in the model you should enter the schema definition for the table like this:

[Table("TableName", Schema = "SchemaName")]
public class TableName
{

}

You have to define both the schema and the table in two different places.

the context defines the schema

public class BContext : DbContext
{
    public BContext(DbContextOptions<BContext> options) : base(options)
    {
    }

    public DbSet<PriorityOverride> PriorityOverrides { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.HasDefaultSchema("My.Schema");

        builder.ApplyConfiguration(new OverrideConfiguration());
    }
}

and for each table

class PriorityOverrideConfiguration : IEntityTypeConfiguration<PriorityOverride>
{
    public void Configure(EntityTypeBuilder<PriorityOverride> builder)
    {
        builder.ToTable("PriorityOverrides");
        ...
    }
}

For what it is worth, I wanted to mention that in my case, the problem was coming from an AFTER INSERT Trigger!

These are not super visible so you might be searching for a while!

In EF (Core) configuration (both data annotations and fluent API), the table name is separated from the schema.

Remove the "dbo." from the table name and use the ToTable overload with name and schema arguments:

.ToTable("MyUsers", "dbo");

Or taking into account that dbo is the default schema (if not configured differently), simply:

.ToTable("MyUsers");

As it is currently, it considers table dbo.dbo.MyUsers which of course does not exist.

The solution is very simple.

Just run the migration. Make sure you have the migrations folder with the code. Then on the Configure method of startup, put this code first in you method body:

using (IServiceScope scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
            {
                scope.ServiceProvider.GetService<FRContext>().Database.Migrate();
            }

This update the database, but its base in the migrations folder. I created the database, but if it does not find the migration folder with the files, it will create the database without tables, and you app will break at runtime.

Use table annotation in your class and use the name same as the name of view or table in the database

[Table("tbl_Sample")]
public class tblSample
{
   //.........
}

在上下文定义中,每个上下文类只定义两个 DbSet 上下文。

当我查看正在测试的项目的配置时,我的测试中的数据库配置错误。

In my case, I put the wrong ConnectionString name in connectionString Configuration in Startup.cs, Your connectionstring name in startup.cs and appsettings.json must be the same.

In appsettings.json: 在此处输入图像描述

In startup.cs: 在此处输入图像描述

Thats why when i made query using this context, it found no connectionstring from the appsettings when there was a wrong name. Consequently, it results Invalid Object Name 'a db table name' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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