看了很多相似的话题后,我无法解决这个问题......
我有下一个型号......
人: Id,姓名,城市
技能: Id,描述,Person_Id
一个人可以拥有0或更多技能,但技能可以拥有1个人。
我的表单包含TextBoxs Name和City,以及与Person.Skills绑定的GridControl
在此GridControl中,您可以删除,添加或编辑行。
问题是当我删除一行并保存更改时,我收到了下一个错误:
“来自'PersonsSkills'AssociationSet的关系处于'已删除'状态。”
当我运行方法DbContext.Skills.Remove(技能)都工作正常,但这是最好的方法吗? 我必须手动删除所有行吗?
码:
Private DbContext As New Model() Private person As Person 'Load Person to edit Private Sub loadPerson() Dim person As Person = (From p In DbContext.Persons Where p.Id = IdPerson Select p).First() End Sub NameTextBox.Text = person.Name CityTextBox.Text = person.City SkillsBindingSource.DataSource = person.Skills.ToList() 'Update changes Private Sub save() person.Name = NameTextBox.Text person.City = CityTextBox.Text person.Skills = Ctype(SkillsBindingSource.DataSource, List(Of Skill)) End Sub DbContext.SaveChanges()感谢帮助!
PD:抱歉我的baaad英语...
After read a lot of similary topics, I can't resolve this problem...
I have next model...
Persons: Id, Name, City
Skills: Id, Description, Person_Id
A person can have 0 o more skills, but a skill can have 1 person.
My form contains TextBoxs Name and City, and a GridControl bound with Person.Skills
In this GridControl y can delete, add or edit rows.
The problem is when I delete a row and save changes, i recieved next error:
"A relationship from the 'PersonsSkills' AssociationSet is in the 'Deleted' state."
When I run method DbContext.Skills.Remove(skill) all work fine, but is this the best way? I have to do it manually for all rows deleted?
Code:
Private DbContext As New Model() Private person As Person 'Load Person to edit Private Sub loadPerson() Dim person As Person = (From p In DbContext.Persons Where p.Id = IdPerson Select p).First() End Sub NameTextBox.Text = person.Name CityTextBox.Text = person.City SkillsBindingSource.DataSource = person.Skills.ToList() 'Update changes Private Sub save() person.Name = NameTextBox.Text person.City = CityTextBox.Text person.Skills = Ctype(SkillsBindingSource.DataSource, List(Of Skill)) End Sub DbContext.SaveChanges()Thanks for help!
PD: Sorry for my baaad english...
最满意答案
解决了!
删除行时,我运行:
DbContext.Skills.Remove(Ctype(SkillsDataBinding.Current, Skill))之后,在“保存”过程中,设置如下导航属性:
person.Skills = Ctype(SkillsDataBinding.DataSource, List(Of Skill))这很简单,但我是一个菜鸟。
Solved!
When a row is deleted, I run:
DbContext.Skills.Remove(Ctype(SkillsDataBinding.Current, Skill))After, in the "Save" procedure, set the navigation property like this:
person.Skills = Ctype(SkillsDataBinding.DataSource, List(Of Skill))It was simple, but I'm a noob.
更多推荐
发布评论