我正在尝试使用ASP MVC从ViewModel向数据库添加记录。该代码仅允许添加某些模型。其他人抛出异常:
存储更新,插入或删除语句会影响意外的行数(0)。 自实体加载后,实体可能已被修改或删除。 刷新ObjectStateManager条目。
查看型号:
public class FamilyApplicationViewModel
{
public FamilyApplicationViewModel()
{
}
public int ID { get; set; }
public Family Family { get; set; }
public FamilyApp FamilyApp { get; set; }
public List<FamilyIncome> FamilyIncomes { get; set; }
public List<FamilyMember> FamilyMembers { get; set;}
}
获取控制器:
public ActionResult Create()
{
return View();
}
此控制器有效:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FamilyApplicationViewModel model)
{
var user = "Sam";
model.Family.CreatedBy = user;
model.Family.ModifiedBy = user;
//all other props set in view
var fam = db.Families.Add(model.Family);
db.SaveChanges();
return RedirectToAction("Index");
}
此控制器没有:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FamilyApplicationViewModel model)
{
var user = "Sam";
model.Family.CreatedBy = user;
model.Family.ModifiedBy = user;
var app = new FamilyApp();
app.AppYear = DateTime.Now.Year;
app.ModifiedBy = user;
app.CreatedBy = user;
// other family app props set
model.Family.FamilyApps.Add(app);
db.Families.Add(model.Family);
db.SaveChanges();
}
此控制器没有:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FamilyApplicationViewModel model)
{
var user = "Sam";
model.Family.CreatedBy = user;
model.Family.ModifiedBy = user;
var fam = db.Families.Add(model.Family);
db.SaveChanges();
model.FamilyApp.AppYear = DateTime.Now.Year;
model.FamilyApp.ModifiedBy = user;
model.FamilyApp.CreatedBy = user;
var app = model.FamilyApp;
model.FamilyApp.FamilyId = fam.FamilyId;
db.FamilyApps.Add(app);
db.SaveChanges(); ///////Exception Here///////
}
此控制器没有:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FamilyApplicationViewModel model)
{
var user = "Sam";
model.Family.CreatedBy = user;
model.Family.ModifiedBy = user;
model.FamilyApp.AppYear = DateTime.Now.Year;
model.FamilyApp.ModifiedBy = user;
model.FamilyApp.CreatedBy = user;
var app = model.FamilyApp;
model.Family.FamilyApps.Add(app);
db.Families.Add(model.Family);
db.SaveChanges(); ///////Exception Here///////
}
此控制器没有:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FamilyApplicationViewModel model)
{
var user = "Sam";
model.FamilyApp.AppYear = DateTime.Now.Year;
model.FamilyApp.ModifiedBy = user;
model.FamilyApp.CreatedBy = user;
model.FamilyApp.FamilyId = 1; //this does exist in db
db.FamilyApps.Add(model.FamilyApp);
db.SaveChanges(); ///////Exception Here///////
}
此控制器没有:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FamilyApplicationViewModel model)
{
var user = "Sam";
model.FamilyApp.AppYear = DateTime.Now.Year;
model.FamilyApp.ModifiedBy = user;
model.FamilyApp.CreatedBy = user;
model.FamilyApp.FamilyId = 1; //this does exist in db
db.FamilyApps.Add(model.FamilyApp);
try
{
db.SaveChanges();
}
catch
{
((IObjectContextAdapter)db).ObjectContext.Refresh(RefreshMode.ClientWins, db.Families);
((IObjectContextAdapter)db).ObjectContext.Refresh(RefreshMode.ClientWins, db.FamilyApps);
db.SaveChanges(); ///////Exception Here///////
}
}
我还看到另一个显示为db.Refresh
的Refresh方法,但我无法使用。
我已经研究过:
这些链接都没有帮助。我已经尝试过将隐藏字段添加到我的视图中。
@Html.HiddenFor(m => m.ID);
@Html.HiddenFor(m => m.Family.FamilyId);
@Html.HiddenFor(m => m.FamilyApp.FamilyAppId);
@for (int i = 0; i < 10; i++)
{
@Html.HiddenFor(m => m.FamilyIncomes[i].FamilyIncomeId);
@Html.HiddenFor(m => m.FamilyMembers[i].FamilyMemberId);
}
有谁知道如何修复此异常?
答案 0 :(得分:0)
我相信您正在寻找的正确方法是这样的: (请注意,我在stackoverflow上写了这个,但没有运行代码)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FamilyApplicationViewModel model)
{
var user = "Sam";
model.Family.CreatedBy = user;
model.Family.ModifiedBy = user;
model.Family.FamilyApp = new FamilyApp();
model.Family.FamilyApp.AppYear = DateTime.Now.Year;
model.Family.FamilyApp.ModifiedBy = user;
model.Family.FamilyApp.CreatedBy = user;
db.Family.Add(model.Family);
db.SaveChanges();
}
如果我是正确的,Family和FamilyApp有一对一的关系。您可以轻松创建整个对象结构,然后添加&#34; root&#34;对象,或者只是其中一个对象,也应该添加其他对象。
答案 1 :(得分:0)
我通过将EF版本从5更新到6来解决此问题。