2

I keep getting error when I try to access a model from an edit or details action.

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

First I tried adding a migration even though I was sure I hadn't changed anything. Still recieved the same error after an update-database.

Then I removed all the migrations and the database and started a clean database with an inital migration and update. Same error. Nothing was changed.

Model is:

public class InjuriesContext : DbContext
    {
        public InjuriesContext()
            : base("DBCon")
        {
        }

        public DbSet<Patient> Patients { get; set; }

        public DbSet<Injury> Injuries { get; set; }
    }

    public class Injury
    {
        public int Id { get; set; }
        public string Type { get; set; }
        public int PatientId { get; set; }
    }

Here is controller --

public ActionResult Edit(int id = 0)
        {
            Injury injury = db.Injuries.Find(id);
            if (injury == null)
            {
                return HttpNotFound();
            }
            return View(injury);
        }

It errors on the injuries.find. I do not have any injuries entered so I expect it to return a 404 like my other controllers but it doesn't like something about this. The only difference between this and my other models is the y to ies for plural. Does Entity Framework not handle this?

Xaxum
  • 3,545
  • 9
  • 46
  • 66
  • What do you mean by "an inital migration and update"? Don't you want you map the table name manually? (Although that would only evade the issue). – Gert Arnold Jan 05 '13 at 22:00
  • @GertArnold I ran and intial add migration and update database through entity framework so it would create the tables. I am not sure what you mean my map the table name manually. – Xaxum Jan 08 '13 at 19:02

2 Answers2

1

There should not be any plural restriction, as you defined everything clearly in your classes anyway.

Have you created the Injuries table?

I belive the table Injury will get created automatically. the variable injury might be a bit close, but I have to test this myself.

Rather try:

public class Injury
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Type { get; set; }
    [Required]
    public int PatientId { get; set; }
}


    private InjuriesContext db = new InjuriesContext();
    Injury objInjury = db.Injuries.Find(id);
    if (objInjury == null)
    {
        return HttpNotFound();
    }
    return View(objInjury);

Hope this helps

ShaunOReilly
  • 2,186
  • 22
  • 34
  • Yes EF created the Injuries table when I ran the initial migration. I tried the above and got the same error. I am at a loss. I just created a different controller, without the ies and it works fine. – Xaxum Jan 08 '13 at 18:57
  • Strike the last. Having the same issue on a controller/model without the ies and just S. Not sure what happened. The tables created have all the correct fields so I don't know what it thinks has changed? – Xaxum Jan 08 '13 at 19:54
  • Thanks did find out there is no issue with the plural as mentioned here but the root issue is answered above. +1 for the help. – Xaxum Jan 09 '13 at 20:23
0

It turns out my issue was with multiple contexts. I thought you had to create a separate context for each model class. Apparently Entity Framework needs one context. I went through and created a class for my context and put all my DBsets in that class.

    public class ProjContexts : DbContext
    {
         public ProjContexts()
            : base("ProjDBCon")
        {
        }

        public DbSet<Patient> Patients { get; set; }
        public DbSet<PreHosp> PreHosps { get; set; }
        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Injury> Injuries { get; set; }

    }
}

Then I removed all the migrations as per this post and enabled the migrations again did an add migration and update then I got the expected result.

Bottom Line--- Don't have multiple context classes in your project. Not sure if this is possible but after changing the above everything is working as expected. Not sure why it was working when I had two separate contexts and added the third? Maybe because they had foreign keys with one another?

Community
  • 1
  • 1
Xaxum
  • 3,545
  • 9
  • 46
  • 66