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

C#Sqlasp.net Mvc-3Entity Framework

C# Problem Overview


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.IQueryable1[MySiteCreator.Models.User], Int32)' method, and this method cannot be translated into a store expression.`

Can anyone help me?
Thank you!

C# Solutions


Solution 1 - C#

>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.

Solution 2 - C#

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; }        
}

Solution 3 - C#

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.

Solution 4 - C#

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>();
 }

Solution 5 - C#

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

EDIT: Updated link.

Solution 6 - C#

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:

enter image description here

Solution 7 - C#

My fix was as simple as making sure the correct connection string was in ALL appsettings.json files, not just the default one.

Solution 8 - C#

Instead of

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

Try:

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

even if your table name is dbo.BaseCs

Solution 9 - C#

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.

Solution 10 - C#

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
{

}

Solution 11 - C#

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");
        ...
    }
}

Solution 12 - C#

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!

Solution 13 - C#

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.

Solution 14 - C#

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.

Solution 15 - C#

In the context definition, define only two DbSet contexts per context class.

Solution 16 - C#

Wrong DB configuration in my tests while I was looking at the configuration from the project being tested.

Solution 17 - C#

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: enter image description here

In startup.cs: enter image description here

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'.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark SegalView Question on Stackoverflow
Solution 1 - C#Jeff OgataView Answer on Stackoverflow
Solution 2 - C#acarlonView Answer on Stackoverflow
Solution 3 - C#sfsView Answer on Stackoverflow
Solution 4 - C#ToffeeView Answer on Stackoverflow
Solution 5 - C#murugeView Answer on Stackoverflow
Solution 6 - C#ElnoorView Answer on Stackoverflow
Solution 7 - C#iKnowNothingView Answer on Stackoverflow
Solution 8 - C#Wai NgView Answer on Stackoverflow
Solution 9 - C#abdelgribView Answer on Stackoverflow
Solution 10 - C#aliView Answer on Stackoverflow
Solution 11 - C#Carlo V. DangoView Answer on Stackoverflow
Solution 12 - C#jsgoupilView Answer on Stackoverflow
Solution 13 - C#Bhadresh PatelView Answer on Stackoverflow
Solution 14 - C#Sterling DiazView Answer on Stackoverflow
Solution 15 - C#Icemark MuturiView Answer on Stackoverflow
Solution 16 - C#BluebaronView Answer on Stackoverflow
Solution 17 - C#Mohammad Sadiqur RahmanView Answer on Stackoverflow