0

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.

2
  • Multiple projects in your solution perhaps? Commented Jun 13, 2014 at 0:36
  • Yeah, 3, Domain, Core, and GUI (MVC5) Commented Jun 13, 2014 at 8:01

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So, I come to the computer in the morning, turn everything on. Don't touch a thing and type Enable-Migrations, works straight off the bat!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.