0
<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および_toUpdateArt.Supplier=オプションの関係を削除するものはありません。

しかし、それは私がそれをデバッグするとき、そして時にはコマンドのいくつかのループの後、関係が実際に削除されてデータベースに保存される前にのみ機能します。

「関数」にブレークポイントがない場合、リレーションシップがヌルになることはありません。

誰かがこれとこれを修正する方法についての説明がありますか?

4

1 に答える 1

2

DepartmentがすでにNothing(null)の場合、Nothingに設定しても効果がない可能性があります。デバッガーがプロパティにアクセスして遅延読み込みを引き起こすため、デバッガーで機能していることがわかります。この後、プロパティはNothingではなくなるため、Nothingに設定すると、関係が削除されたことが検出されます。

これを処理する方法はいくつかあります。

  • FKをエンティティ内のプロパティにマッピングしていることを確認してから(つまり、FK関係を使用して)、ナビゲーションプロパティをnullに設定するのではなく、FKをnullに設定します。この場合、FKは関係のトークンとして機能しているため、関連するエンティティをロードしなくても明確に変更できます。
  • プロパティがnullにならないように、常に関連エンティティをロードしてください。これは、インクルードまたは遅延読み込みを使用して行うことができます。
  • .NET 4.5でEF5を使用している場合は、完全な変更追跡プロキシの使用を開始できます。その場合、プロパティをNothingに設定すると、既にNothingであっても、プロキシによって検出されます。ただし、変更追跡プロキシには複雑な問題があり、これは.NET4.5を使用する場合のFK関係でのみ機能することに注意してください。
于 2012-06-07T15:19:24.077 に答える