由于实体框架的状态,在这里我们去的代码先删除孩子的......实体框架代码首先,通过“先典”更新父
public class BaseModel
{
[Key]
public Guid Id { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateChanged { get; set; }
public BaseModel()
{
this.Id = Guid.NewGuid();
this.DateCreated = DateTime.Now;
this.DateChanged = DateTime.Now;
}
}
public class Association: BaseModel
{
public string Name { get; set; }
public string Type { get; set; }
public virtual List<Rule> Rules { get; set; }
public Association()
: base()
{
}
}
public class Rule: BaseModel
{
[ForeignKey("Association")]
public Guid AssociationId { get; set; }
//[Required]
public virtual Association Association { get; set; }
//[Required]
public string Name { get; set; }
public string Expression { get; set; }
public virtual List<Action> Actions { get; set; }
public Rule()
: base()
{
}
}
public class Action: BaseModel
{
public string Name { get; set; }
public string ActionType { get; set; }
[ForeignKey("Rule")]
public Guid RuleId { get; set; }
public virtual Rule Rule { get; set; }
public int Order { get; set; }
public Action()
: base()
{
}
}
因此,这些都是我的四个模型类,使用实体框架代码第一。 每个都从基类继承,所以它们都有一个Id Guid作为主键。
一个关联有一个规则列表。 (规则具有FK给协会) 规则与动作列表一样。 (动作有FK来规则)
我想要做的只是改变并保存最向上的class = Association。 例如删除规则时,我想这个代码工作:
public ActionResult DeleteRule(Guid assId, Guid ruleId)
{
Association ass = this.DataContext.Associations.FirstOrDefault(a => a.Id == assId);
ass.Rules.RemoveAll(r => r.Id == ruleId);
this.DataContext.SaveChanges();
return RedirectToAction("Index");
}
在这给我这个错误context.savechanges: “操作失败:关系不能被改变,因为一个或更多的外键属性是不可空的。当对关系进行更改时,相关的外键属性将设置为空值。如果外键不支持空值,则必须定义新的关系,必须为外键属性指定另一个非空值,或者必须删除不相关的对象。
删除操作时也会发生此错误。
有没有办法改变最上层(关联)对象,只有改变这个关联的东西。 我不想说context.Rules.remove(...)或context.actions.remove(...)
这里的源:http://server.thomasgielissen.be/files/mvctesting.zip 你需要VS2012,所有的NuGet包包含在ZIP和你应该能够建立并运行项目。
在此先感谢您的反馈!
格尔茨, 托马斯
您需要重新格式化这个问题。简化您的问题描述并在帖子中包含最少的相关代码。就目前而言,你很难获得帮助。 – Michal
问题已更新!,希望我现在可以获得帮助:) –