[SOLVED] "How to fix" SqlException: Invalid object name 'Orders'. databatase location?

Issue

I followed the instructions in this video https://docs.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-aspnet-core-ef-step-03?view=vs-2019 to try to add a products database. and after some (inital bugginess I first crashed and the website asked me to apply migrations and i hit apply) an then it eventually worked.

The instructions tell you to create a new models folder and then add a class to it. then in the pages fold add a new folder for your model followed by a new scaffolded item (razer page with entity fw (crud)). I then added the following code to make sure the database gets created in my program.cs to make sure the database is created

  var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService<EisenMNdbContext>();
                    context.Database.EnsureCreated();
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService<ILogger<Program>>();
                    logger.LogError(ex, "An error occurred creating the DB.");
                }
            }

            host.Run();

I that and it worked for products.
I then wanted to add a second model/app called orders so I added a nother class to models, and then created another folder and added a scaffolded item doing the same process but this time i get this:

SqlException: Invalid object name ‘Order’.

System.Data.SqlClient.SqlCommand+<>c.b__122_0(Task result)

I noticed in links like this one

SqlException (0x80131904): Invalid object name 'dbo.Categories'

that people often had the database table as the wrong name. is there a place where I can edit that?list of file directories

here is some more of the error log if that helps:

qlException: Invalid object name 'Orders'.

    System.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__122_0(Task<SqlDataReader> result)
    System.Threading.Tasks.ContinuationResultTaskFromResultTask<TAntecedentResult, TResult>.InnerInvoke()
    System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
    System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot)
    Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary<string, object> parameterValues, CancellationToken cancellationToken)

my db context looks like this if that matters

    {
        public EisenMNdbContext (DbContextOptions<EisenMNdbContext> options)
            : base(options)
        {
        }

        public DbSet<EisenMN.Models.Product> Products { get; set; }

        public DbSet<EisenMN.Models.Order> Orders { get; set; }


    }
}

Solution

the link from Migg in the comments helped! docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/… use a PowerShell and make sure you’ve properly applied migrations.

Answered By – Samuel Wakeman

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *