[英]An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll
I'm trying to update my database. 我正在尝试更新数据库。 The exception "An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code" occurs at db.SaveChanges(). db.SaveChanges()发生异常“在EntityFramework.dll中发生了'System.Data.Entity.Infrastructure.DbUpdateException类型的异常,但未在用户代码中处理”。
I get the last id from the table "Ekipa", and set the new id as "value + 1". 我从“ Ekipa”表中获取了最后一个ID,并将新ID设置为“ value + 1”。 I set all the properties, which I get from the form. 我设置了所有属性,这些属性是从表单获得的。 Then I set the currentUser's foreignKey, which is the same as the new entry id. 然后,设置currentUser的foreignKey,它与新条目ID相同。 I set the state as modified, but I still get the error. 我将状态设置为已修改,但仍然收到错误。 Does anyone see any problems with it? 有人看到它有任何问题吗? It used to work fine, but I recently changed the foreignKey from being and "int", to "int?" 它曾经可以正常工作,但是最近我将foreignKey从being和“ int”更改为“ int?”。 (so it's now nullable). (因此现在可以为空)。 Is that the problem, or am I missing something here? 是这个问题,还是我在这里错过了什么?
int value = int.Parse(db1.Ekipa
.OrderByDescending(p => p.id)
.Select(r => r.id)
.First().ToString());
int geslo = 0;
int NovaId = value;
Int32.TryParse(EkipaGeslo, out geslo);
Ekipa nova = new Ekipa();
nova.id = value + 1;
nova.Ime = EkipaIme;
nova.Geslo = geslo;
nova.LeaderID = currentUser.AccountID;
currentUser.EkipaId = nova.id;
db.Entry(currentUser).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
db1.Ekipa.Add(nova);
db1.SaveChanges();
Apparently my "Ekipa" table was somehow deleted, and nothing was refreshing. 显然,我的“ Ekipa”表已被删除,没有任何刷新。 Fixed it by recreating it 通过重新创建来修复它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.