Search code examples
entity-framework-coreazure-web-app-servicetable-renameinvalid-object-name

EF core Invalid object name after table rename in App Service stg env


I'm running my app locally, in dev env and also in prd env without a problem, but in stg env I get this "Invalid object name 'bls.TaskFeedReactions" exception..

I renamed the table from "TaskFeedReactions" to "FeedReactions", the migrations dropped the table "TaskFeedReactions" and created the new one "FeedReactions", in every env DB (dev(used also by localhost), stg and prd) I have the table "FeedReactions", also in the DbContext I have this:

        EntityTypeBuilder<FeedReactionModel> feedReactions = modelBuilder.Entity<FeedReactionModel>();

        feedReactions.ToTable("FeedReactions", BLSchema);
                            .
                            .

Why is stg environment creating a query that uses the old name "TaskFeedReactions" and how can I fix it?

Bests


Solution

  • Somehow EF Core had cached the query in this environment in a way that restarting or redeploying the app was not cleaning the cached query and recreating the new query with the table name updated.

    So to solve this I commented the part of the query that accessed the renamed table:

            return await context.TaskFeedMessages
                .Where(x => x.TaskId == taskId)
                .Select(x => new TaskFeedModel
                {
                    Id = x.Id,
                    CreatorId = x.CreatorId,
                    TaskId = x.TaskId,
                    MessageType = x.MessageType,
                    Text = x.Text,
                    Status = x.Status,
                    IsImportant = x.IsImportant,
                    //ReactionsDict = new Dictionary<string, HashSet<string>>(
                    //    x.Reactions.GroupBy(Y => Y.Reaction)
                    //    .Select(y => new KeyValuePair<string, HashSet<string>>(
                    //            y.Key, y.Select(z => z.UserId).ToHashSet()))),
                    CreatedAt = x.CreatedAt
                })
                .OrderByDescending(x => x.CreatedAt)
                .Skip(skip)
                .Take(take)
                .ToListAsync();
        
    

    Redeployed it in stg env and and then removed the comments and redeployed again.

    It solved the problem for me.