Skip to content

Updated project from RC1 to RC2. Invalid object name 'TableName' #4979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Bartmax opened this issue Apr 5, 2016 · 4 comments
Closed

Updated project from RC1 to RC2. Invalid object name 'TableName' #4979

Bartmax opened this issue Apr 5, 2016 · 4 comments

Comments

@Bartmax
Copy link

Bartmax commented Apr 5, 2016

and when launching I get this error:

info: Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory[1]
      Executed DbCommand (17ms) [Parameters=[@__p_0='?'], CommandType='Text', CommandTimeout='30']
      SELECT TOP(@__p_0) [p].[Id], [p].[Body], [p].[CategoryId], [p].[CreatedAt], [p].[Date], [p].[Flavor], [p].[IsDeleted], [p].[IsPublished], [p].[PublishedAt], [p].[Summary], [p].[ThumbnailId], [p].[Title], [a].[Id], [a].[ContentType], [a].[Filename], [a].[OriginalFilename], [a0].[Id], [a0].[Name], [a0].[Slug]
      FROM [Announcements] AS [p]
      LEFT JOIN [AnnouncementThumbnail] AS [a] ON [p].[ThumbnailId] = [a].[Id]
      INNER JOIN [AnnouncementCategories] AS [a0] ON [p].[CategoryId] = [a0].[Id]
      WHERE [p].[IsDeleted] = 0 AND [p].[IsPublished] = 1
      ORDER BY [p].[Date] DESC
fail: Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[1]
      An exception occurred in the database while iterating the results of a query.
      System.AggregateException: One or more errors occurred. (One or more errors occurred. (One or more errors occurred. (One or more errors occurred. (Invalid object name 'Announcements'.)))) ---> System.AggregateException: One or more errors occurred. (One or more errors occurred. (One or more errors occurred. (Invalid object name 'Announcements'.))) ---> System.AggregateException: One or more errors occurred. (One or more errors occurred. (Invalid object name 'Announcements'.)) ---> System.AggregateException: One or more errors occurred. (Invalid object name 'Announcements'.) ---> System.Data.SqlClient.SqlException: Invalid object name 'Announcements'.

database has correct schema/data

Modification i made to the project:

on project json.

"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-*",`
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-*",
"Microsoft.EntityFrameworkCore.Commands": "1.0.0-*",

startup.cs:

services.AddEntityFramework()
                .AddEntityFrameworkSqlServer()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

I had to add a new constructor to dbcontext like this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions options):base(options)
    {

    }
    // everything else ...
}

and I don't remember anything else I changed (related to ef)

dnvm list

Active Version           Runtime Architecture OperatingSystem Alias
------ -------           ------- ------------ --------------- -----
       1.0.0-rc1-update1 clr     x64          win
       1.0.0-rc1-update1 clr     x86          win
       1.0.0-rc1-update1 coreclr x64          win
       1.0.0-rc1-update1 coreclr x86          win
       1.0.0-rc2-16128   clr     x64          win
       1.0.0-rc2-16128   clr     x86          win             rc2
       1.0.0-rc2-16128   coreclr x64          win             core64
       1.0.0-rc2-16128   coreclr x86          win             core
  *    1.0.0-rc2-20221   clr     x86          win             default

dotnet --info

.NET Command Line Tools (1.0.0-beta-002203)

Product Information:
 Version:     1.0.0-beta-002203
 Commit Sha:  f6fa1cf060

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.10586
 OS Platform: Windows

testing running it doing dotnet run.

@Bartmax Bartmax changed the title Updated project from RC1 to RC2. Updated project from RC1 to RC2. Invalid object name 'TableName' Apr 5, 2016
@divega
Copy link
Contributor

divega commented Apr 5, 2016

@Bartmax this is just a guess but is the correct name of the table "Announcement" (without the "s")? For RC2 we are making a change to automatically infer the default names from the names of the DbSet<TEntity> properties of the DbContext so you may need to explicitly specify the name of the table if want to keep the same name you had in RC1.

@rowanmiller
Copy link
Contributor

BTW if you want to go back to the naming from RC1, so that your model and database work together without issues, then just put this code at the start of OnModelCreating(...) on your context:

foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
    entity.Relational().TableName = entity.ClrType.Name;
}

@Bartmax
Copy link
Author

Bartmax commented Apr 5, 2016

That was exactly my issue. Thanks.

Now, I think this will be good to be mentioned on a migration document.

I want to be able to use the new RC2 table name conventions, what is the suggested approach for my scenario here? Create a new empty migration and add the renames by hand? will that work?

@Bartmax
Copy link
Author

Bartmax commented Apr 5, 2016

just in time an announcement were created. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants