MVC Application using old DBContext

时间:2015-07-28 16:17:17

标签: asp.net-mvc entity-framework asp.net-mvc-4

I went through the Movies tutorial on asp.net, which went fine. Created the application using a local SQL Server instance, database was created flawlessly, and all functionality worked as designed. I created a new, entirely separate application without importing or copying anything from the Movies application. Defined a new model and DBContext, new controller, etc. When I try to execute the application for the same time I get the following error:

The model backing the 'OnlineAppDBContext' context has changed since the >database was created. Consider using Code First Migrations to update the >database (http://go.microsoft.com/fwlink/?LinkId=238269).

I went through the exercise of adding the migrations and noticed in the init migration I added that it is referencing the Movie model and database:

public override void Up()
{
    CreateTable(
            "dbo.OnlineApps",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    FirstName = c.String(),
                    MiddleInitial = c.String(),
                    LastName = c.String(),
                    PreviousFullName = c.String(),
                    Gender = c.String(),
                    BirthDate = c.DateTime(nullable: false),
                    SSN = c.String(),
                    Tobacco = c.Boolean(nullable: false),
                    Address1 = c.String(),
                    Address2 = c.String(),
                    City = c.String(),
                    County = c.String(),
                    State = c.String(),
                    ZipCode = c.String(),
                    HomePhone = c.String(),
                    WorkPhone = c.String(),
                    CellPhone = c.String(),
                    Email = c.String(),
                    PCPChoice = c.String(),
                })
            .PrimaryKey(t => t.ID);

        DropTable("dbo.Movies");
    }

    public override void Down()
    {
        CreateTable(
            "dbo.Movies",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    Title = c.String(maxLength: 60),
                    ReleaseDate = c.DateTime(nullable: false),
                    Genre = c.String(nullable: false, maxLength: 30),
                    Price = c.Decimal(nullable: false, precision: 18, scale: 2),
                    Rating = c.String(maxLength: 5),
                })
            .PrimaryKey(t => t.ID);

        DropTable("dbo.OnlineApps");
    }

The Up() method looks to do the correct table structure for my new model, but then tries to drop dbo.Movies. The Down() method table structure is based on the Movie model, but I have no idea how it is aware of that, and then tries to drop the dbo.OnlineApps table.

I searched through all files in the new application and just for fun rebooted the entire machine, but still having the same problem. Any ideas?

EDIT On a hunch I created a new database on my server. The movies application used a database called 'Sandbox'. I created an 'OnlineApplication' database on the same server, changed the connection string in the new application, and when it ran the objects were created without error. While this got me through the issue I'm not overly satisfied with the answer. Why would the connection string convey any relation to a model from a completely different application? Is this stored somewhere it can be edited/deleted? Can I not define multiple contexts to the same database?

0 个答案:

没有答案