EF - 仅在被断点击中时删除关系

时间:2012-06-07 14:33:41

标签: vb.net entity-framework entity-framework-4

<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

_toUpdateArt.Departement = Nothing and _toUpdateArt.Supplier =没有什么可以删除可选关系。

但它只在我调试它时有效,有时在命令的几个循环之后,在实际删除关系之前它存储在数据库中。

当“功能”上没有断点时,关系将永远不会被取消。

有没有人对此有解释以及如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

如果Department已经为Nothing(null),则很可能将其设置为Nothing将无效。我怀疑你看到它在调试器中工作,因为调试器访问属性并导致它延迟加载。在此之后,该属性不再是Nothing,因此将其设置为Nothing被检测为删除关系。

有几种方法可以解决这个问题:

  • 确保将FK映射到实体中的属性(即使用FK关系),然后将FK设置为​​null,而不是将navigation属性设置为null。在这种情况下,FK充当关系的标记,以便可以明确地更改它,而不需要加载相关实体。
  • 始终加载相关实体,以使该属性永远不为null。您可以使用Include或延迟加载来执行此操作。
  • 如果您在.NET 4.5上使用EF5,那么您可以开始使用完整更改跟踪代理,在这种情况下,将属性设置为Nothing将被代理检测到,即使它已经是Nothing。但请注意,更改跟踪代理存在复杂性,这仅适用于使用.NET 4.5时的FK关系。