是否有模型数据库上下文y将在mvc4 EF IES复数问题 [英] Is there a plural issue for models database context y to ies in mvc4 EF
问题描述
我不断收到错误,当我尝试从编辑或细节动作访问模式。
I keep getting error when I try to access a model from an edit or details action.
该模式支持了InjuriesContext语境自从改变
数据库已创建。考虑使用code首先迁移到更新
数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
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.
然后我删除了所有的迁移和数据库,并与inital迁移和更新开始一个干净的数据库。同样的错误。什么都没有改变。
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.
模式是:
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; }
}
下面是控制器 -
public ActionResult Edit(int id = 0)
{
Injury injury = db.Injuries.Find(id);
if (injury == null)
{
return HttpNotFound();
}
return View(injury);
}
这对injuries.find错误。我没有进入,所以我希望它返回一个404就像我的其他控制器的任何伤害,但它不喜欢的东西这件事。这和我的其他车型之间的唯一区别是y到IES为复数。难道实体框架不处理呢?
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?
推荐答案
原来,我的问题是与多个上下文。我以为你要创建的每个模型类一个单独的上下文。显然实体框架需要一个上下文。我的经历和创造一类为我的背景下,把我所有的DBsets该类别。
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?
这篇关于是否有模型数据库上下文y将在mvc4 EF IES复数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!