¿Hay un problema plural para el contexto de la base de datos de modelos y a ies en mvc4 EF?

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.

El modelo es:

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?

preguntado el 03 de enero de 13 a las 18:01

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). -

@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. -

2 Respuestas

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);

Espero que esto ayude

Respondido el 04 de enero de 13 a las 01:01

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

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

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

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 esta publicación 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?

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.