Arianit
Arianit

Reputation: 553

Conflict with database table and key names after migration. Asp.Net MVC 5 EF 6

This is the code that migration generates for me:

public override void Up()
    {
        CreateTable(
            "dbo.ApplicationUserProjects",
            c => new
                {
                    ApplicationUser_Id = c.String(nullable: false, maxLength: 128),
                    Project_ID = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.ApplicationUser_Id, t.Project_ID })
            .ForeignKey("dbo.AspNetUsers", t => t.ApplicationUser_Id, cascadeDelete: true)
            .ForeignKey("dbo.ProjectTbl", t => t.Project_ID, cascadeDelete: true)
            .Index(t => t.ApplicationUser_Id)
            .Index(t => t.Project_ID);

    }

I change the database name to something I like more, also the Key names (column names). Example the database name into "ProjectUsers", and the Keys into "ProjectID" and "UserID". So the new code looks like this:

CreateTable(
            "dbo.ProjectUsers",
            c => new
                {

                    ProjectID = c.Int(nullable: false),
                    UserId = c.String(nullable: false, maxLength: 128),
                })
            .PrimaryKey(t => new { t.ProjectID, t.UserId })
            .ForeignKey("dbo.ProjectTbl", t => t.ProjectID, cascadeDelete: true)
            .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
            .Index(t => t.ProjectID)
            .Index(t => t.UserId);

I update the database by running the migration update, the table and everything gets created with the names I gave to the database and keys. But when I navigate to the page it says: enter image description here Same thing happens when I undo the change for the Database name but keep my own names for they keys.

So why do this happen, what is happening behind the scenes that doesn't want the app to use the new names?

Upvotes: 0

Views: 638

Answers (1)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

As shown on your exception details:

System.Data.SqlClient.SqlException: Invalid object name 'dbo.ApplicationUserProjects'

EF tried translating LINQ query statement to SQL equivalent which uses an object called ApplicationUserProjects (assumed a table), but the corresponding object doesn't exist on your DB since it was not migrated.

Assume code-first migrations in place, since you have created ProjectUsers table with different primary key index name, create proper table mapping inside overridden OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ProjectUsers>().ToTable("ProjectUsers");
}

Then, change all references related with ApplicationUserProjects to ProjectUsers table, including your model if any (e.g. if you have table attribute pointed to ApplicationUserProjects, change to ProjectUsers).

[Table("ProjectUsers")]
public class ProjectUsers
{
    // entity members
}

Note that those actions above better to be done before migration begins, since you should avoid modifying auto-generated migration files.

Your problem statement also supporting primary key problem:

Same thing happens when I undo the change for the Database name but keep my own names for they keys.

IMHO, EF model classes work with specified primary key index defined when the database created, with your own primary key field name substituted original PK field it doesn't match table-to-model relationships anymore, thus returning same exception.

Related problems:

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

Entity Framework code first seems confused on my dbo table names after I tweaked the migration file

Upvotes: 1

Related Questions