Vb.net EF-仅在被断点击中时删除关系 函数编辑(ByVal文章作为FormCollection,可选ByVal DepartmentId作为Integer=0,可选ByVal LeverncierId作为Integer=0)作为ActionResult',ByVal引用供应商ID作为Integer 'Dim Art As Article=ArticleService.GetArticleById(Article.ArticleID) Dim_toUpdateArt As Article=ArticleService.GetArticleById(Article(0)) UpdateModel(_toUpdateArt,文章) “TryUpdateModel(艺术、文章) 如果LeverancierID>0,则 _toUpdateArt.Supplier=LeverancierService.GetLeverancierById(LeverancierID) 其他的 _toUpdateArt.Supplier=此处无 ModelState.Remove(“LeverancierID”) 如果结束 如果部门>0,则 _toUpdateArt.department=DepartmentService.GetDepartmentByID(departmentTID) 其他的 _toUpdateArt.department=此处无任何内容 ModelState.Remove(“部门ID”) 如果结束 如果ModelState.IsValid,则 _toUpdateArt.EditedOn=现在 ArticleService.SaveArticle() 返回重定向操作(“索引”) 其他的 ViewBag.Index=“” ViewBag.Create=“” 'ViewBag.Edit=“” 'ViewBag.StandardValueEnabled ViewBag.Delete=“” ViewBag.Editable=“” 'ViewBag.LocalSearch=“” ViewBag.Departments=DepartmentService.GetDepartments 'ViewBag.StandardValueEnabled=“” ViewBag.MediaShow=“” ViewData(“媒体”)=MediaService.GetMedia.Where(函数(el)el.Reference=Domain.Common.ReferenceTo.Article和el.ReferenceID.Equals(_toUpdateArt.ArticleID)).ToList Dim avw作为新的ViewModels.ArticleViewModel 带avw .Article=\u toUpdateArt .Leveranciers=LeverAnciersService.GetLeveranciers.ToList.Select(函数(dl)使用{_ .Text=dl.RoepNaam_ .Value=dl.LeverancierID_ .Selected=LeverancierID=dl.LeverancierID}).ToList .departments=DepartmentService.GetDepartments.Select(函数(dl)使用{_ .Text=dl.Code_ .Value=dl.DepartmentID_ .Selected=DepartmentID=dl.DepartmentID}).ToList 以 返回视图(avw) 如果结束 端函数
_toUpdateArt.department=Nothing和_toUpdateArt.Supplier=Nothing将删除可选关系 但它只在我调试它时起作用,有时在命令的几个循环之后,在关系被实际删除并存储在数据库中之前 当“函数”上没有断点时,关系将永远不会为空Vb.net EF-仅在被断点击中时删除关系 函数编辑(ByVal文章作为FormCollection,可选ByVal DepartmentId作为Integer=0,可选ByVal LeverncierId作为Integer=0)作为ActionResult',ByVal引用供应商ID作为Integer 'Dim Art As Article=ArticleService.GetArticleById(Article.ArticleID) Dim_toUpdateArt As Article=ArticleService.GetArticleById(Article(0)) UpdateModel(_toUpdateArt,文章) “TryUpdateModel(艺术、文章) 如果LeverancierID>0,则 _toUpdateArt.Supplier=LeverancierService.GetLeverancierById(LeverancierID) 其他的 _toUpdateArt.Supplier=此处无 ModelState.Remove(“LeverancierID”) 如果结束 如果部门>0,则 _toUpdateArt.department=DepartmentService.GetDepartmentByID(departmentTID) 其他的 _toUpdateArt.department=此处无任何内容 ModelState.Remove(“部门ID”) 如果结束 如果ModelState.IsValid,则 _toUpdateArt.EditedOn=现在 ArticleService.SaveArticle() 返回重定向操作(“索引”) 其他的 ViewBag.Index=“” ViewBag.Create=“” 'ViewBag.Edit=“” 'ViewBag.StandardValueEnabled ViewBag.Delete=“” ViewBag.Editable=“” 'ViewBag.LocalSearch=“” ViewBag.Departments=DepartmentService.GetDepartments 'ViewBag.StandardValueEnabled=“” ViewBag.MediaShow=“” ViewData(“媒体”)=MediaService.GetMedia.Where(函数(el)el.Reference=Domain.Common.ReferenceTo.Article和el.ReferenceID.Equals(_toUpdateArt.ArticleID)).ToList Dim avw作为新的ViewModels.ArticleViewModel 带avw .Article=\u toUpdateArt .Leveranciers=LeverAnciersService.GetLeveranciers.ToList.Select(函数(dl)使用{_ .Text=dl.RoepNaam_ .Value=dl.LeverancierID_ .Selected=LeverancierID=dl.LeverancierID}).ToList .departments=DepartmentService.GetDepartments.Select(函数(dl)使用{_ .Text=dl.Code_ .Value=dl.DepartmentID_ .Selected=DepartmentID=dl.DepartmentID}).ToList 以 返回视图(avw) 如果结束 端函数,vb.net,entity-framework,entity-framework-4,Vb.net,Entity Framework,Entity Framework 4,_toUpdateArt.department=Nothing和_toUpdateArt.Supplier=Nothing将删除可选关系 但它只在我调试它时起作用,有时在命令的几个循环之后,在关系被实际删除并存储在数据库中之前 当“函数”上没有断点时,关系将永远不会为空 是否有人对此有解释以及如何解决此问题?如果部门已为空,则很可能将其设置为空将无效。我怀疑您看到它在调试器中工作,因为调试器访问该属性并导致其延迟加载。在此之后,属性不再为Nothing,因此将其设置为Nothing将被检测为删除
是否有人对此有解释以及如何解决此问题?如果部门已为空,则很可能将其设置为空将无效。我怀疑您看到它在调试器中工作,因为调试器访问该属性并导致其延迟加载。在此之后,属性不再为Nothing,因此将其设置为Nothing将被检测为删除关系 有几种方法可以处理此问题:
- 确保将FK映射到实体中的属性(即使用FK关系),然后将FK设置为null,而不是将导航属性设置为null。在这种情况下,FK充当关系的标记,因此可以明确地更改它,而无需加载相关实体
- 始终加载相关实体,以便属性永远不为null。您可以使用Include或延迟加载来实现这一点
- 如果您在.NET 4.5上使用EF5,则可以开始使用完全更改跟踪代理,在这种情况下,代理将检测到将属性设置为Nothing(无),即使该属性已经为Nothing(无)。但是请注意,更改跟踪代理存在复杂性,这仅适用于使用.NET 4.5时的FK关系
<HttpPost()>
Function Edit(<Bind(Prefix:="Article")> ByVal Article As FormCollection, Optional ByVal DepartementID As Integer = 0, Optional ByVal LeverancierID As Integer = 0) As ActionResult ', ByVal ReferenceSupplierID As Integer
' Dim Art As Article = ArticleService.GetArticleById(Article.ArticleID)
Dim _toUpdateArt As Article = ArticleService.GetArticleById(Article(0))
UpdateModel(_toUpdateArt, Article)
' TryUpdateModel(Art, Article)
If LeverancierID > 0 Then
_toUpdateArt.Supplier = LeverancierService.GetLeverancierById(LeverancierID)
Else
_toUpdateArt.Supplier = Nothing 'HERE
ModelState.Remove("LeverancierID")
End If
If DepartementID > 0 Then
_toUpdateArt.Departement = DepartmentService.GetDepartmentByID(DepartementID)
Else
_toUpdateArt.Departement = Nothing 'HERE
ModelState.Remove("DepartmentID")
End If
If ModelState.IsValid Then
_toUpdateArt.EditedOn = Now
ArticleService.SaveArticle()
Return RedirectToAction("Index")
Else
ViewBag.Index = ""
ViewBag.Create = ""
' ViewBag.Edit = ""
'ViewBag.StandardValueEnabled
ViewBag.Delete = ""
ViewBag.Editable = ""
' ViewBag.LocalSearch = ""
ViewBag.Departments = DepartmentService.GetDepartments
' ViewBag.StandardValueEnabled = ""
ViewBag.MediaShow = ""
ViewData("Media") = MediaService.GetMedia.Where(Function(el) el.Reference = Domain.Common.ReferenceTo.Article And el.ReferenceID.Equals(_toUpdateArt.ArticleID)).ToList
Dim avw As New ViewModels.ArticleViewModel
With avw
.Article = _toUpdateArt
.Leveranciers = LeverancierService.GetLeveranciers.ToList.Select(Function(dl) New SelectListItem With { _
.Text = dl.RoepNaam, _
.Value = dl.LeverancierID, _
.Selected = LeverancierID = dl.LeverancierID}).ToList
.Departements = DepartmentService.GetDepartments.Select(Function(dl) New SelectListItem With { _
.Text = dl.Code, _
.Value = dl.DepartmentID, _
.Selected = DepartementID = dl.DepartmentID}).ToList
End With
Return View(avw)
End If
End Function