Callum Linington
Callum Linington

Reputation: 14417

Migrations ASP NET MVC5

I had created my project and configured the user entities.

I had enabled migrations in my project. To test this I simple added a user and then logged into the ASP NET application through the browser. It all worked great.

I added a couple more entities and their corresponding DbSet<T>. I created a controller to manage these entities.

I went to Update-Database using this seed method:

var manager = new UserManager<ChevieUser>(new UserStore<ChevieUser>(new DefaultContext()));

    var people = new List<ChevieUser>
    {
        new ChevieUser { FirstName = "blah", LastName = "blah", UserName="cliningt", Email="[email protected]" }
    };

    people.ForEach(x => manager.Create(x, "password"));

    context.SaveChanges();

    var projects = new List<Project>
    {
        new Project { Name= "MLounge", ProposedDuration = DateTime.Now.AddDays(14), Users = new List<ChevieUser>(){context.Users.FirstOrDefault()}  }
    };

    projects.ForEach(x => context.Projects.AddOrUpdate(p => p.Name, x));

    context.SaveChanges();

    //var targets = new List<Target>
    //{
    //    new Target {Name="Initial Meeting", CompletionDate = DateTime.Now.AddDays(1), ProjectId = context.Projects.FirstOrDefault().Id},
    //    new Target {Name="Pricing", CompletionDate = DateTime.Now.AddDays(2), ProjectId = context.Projects.FirstOrDefault().Id},
    //    new Target {Name="Layout", CompletionDate = DateTime.Now.AddDays(3), ProjectId = context.Projects.FirstOrDefault().Id}
    //};

    //targets.ForEach(x => context.Targets.AddOrUpdate(p => p.Name, x));

    //context.SaveChanges();

It created a project, however, the project had an Id of a empty Guid. Even though I am using the same code to for the Id property as I have done with every other project.

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

The reason why the targets code is commented out is that it can't add three targets with the same Id because the above code isn't working properly.

So I thought I would update my MVC project with the latest nuget packages. This was successful. However, when I came to Update-Database there was an error that migrations was not enabled in my project!

Then after trying to Enable-Migrations it came back with an error that there is no context!

From everything working perfectly fine, to the whole project messing up like this is so bizarre. Out shot of it is, migrations seems to have magically disable itself, but even when it was working, the DatabaseGenerated(DatabaseGeneratedOption.Identity) wasn't working properly.

Upvotes: 0

Views: 215

Answers (1)

AndrewC
AndrewC

Reputation: 6730

It sounds like EF is looking in the wrong project.

Make sure the "Default Project" dropdown in your Package Manager Console window is set to the project that contains your DbContext class. This tells the package manager console which project to execute commands against.

Upvotes: 3

Related Questions