更新时关系处于“已删除”状态(A relationship is in the 'Deleted' state on updating)

编程入门 行业动态 更新时间:2024-10-24 23:22:04
更新时关系处于“已删除”状态(A relationship is in the 'Deleted' state on updating)

看了很多相似的话题后,我无法解决这个问题......

我有下一个型号......

人: 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.

更多推荐

本文发布于:2023-08-02 19:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380692.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   关系   relationship   updating   state

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!