Ajout effectué sans avoir fais de Add
Bonjour,
je me retrouve devant une ineptie. J'ai un objet personne qui est ajouté dans ma table personne alors que je ne fais pas de Add dans cette table.
Je vous met le code ci-dessous car là, je n'y comprend plus rien ...
Cela se produit à ce save change là :
Citation:
vm.Demande.IsDeleted = False
context.DemandeLogement.Add(vm.Demande)
context.SaveChanges()
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
<HttpPost()>
Function Create(ByVal vm As DemandeLogementCreateViewModel, ByVal collection As FormCollection) As ActionResult
Dim Filepath As String = String.Empty
Dim uploadDir As DirectoryInfo = Nothing
Dim targetDir As DirectoryInfo = Nothing
Try
ModelState.Remove("Personne")
ModelState.Remove("TypeDocument")
ModelState.Remove("Demande.Personne.RevenuMensuel")
ModelState.Remove("Demande.Personne.RevenuMensuelConjoint")
ModelState.Remove("Demande.Personne.TotalRevenuMensuel")
If ModelState.IsValid Then
Dim pers As Personne = context.Personne.FirstOrDefault(Function(x) x.Id = vm.Demande.Personne.Id)
If pers Is Nothing Then
context.Personne.Add(vm.Demande.Personne)
End If
If vm.Demande.CritereRecherche Is Nothing Then
context.CritereRecherche.Add(New CritereRecherche With {.EstDemandeMutation = False, .EstUrgent = False})
Else
context.CritereRecherche.Add(vm.Demande.CritereRecherche)
End If
vm.Demande.IsDeleted = False
context.DemandeLogement.Add(vm.Demande)
context.SaveChanges()
Filepath = Server.MapPath("~/Fichiers/" & vm.Demande.AsEnCharge & "/temp/" & vm.Demande.Personne.Nom & "_" & vm.Demande.Personne.Prenom)
targetDir = New DirectoryInfo(Server.MapPath("~/Fichiers/" & vm.Demande.AsEnCharge & "/" & vm.Demande.Personne.Nom & "_" & vm.Demande.Personne.Prenom & "/" & vm.Demande.Id))
If Directory.Exists(Filepath) Then
uploadDir = New DirectoryInfo(Filepath)
For Each item As DirectoryInfo In uploadDir.GetDirectories
For Each subItem As FileInfo In item.GetFiles
'vm.Demande.Document.Add(New Document With {.Chemin = subItem.DirectoryName, .Nom = subItem.Name})
context.Document.Add(New Document With {.Chemin = targetDir.FullName, .Nom = subItem.Name, .IdDemandeLogement = vm.Demande.Id})
Next
Next
context.SaveChanges()
If Not Directory.Exists(targetDir.FullName) Then
Directory.CreateDirectory(targetDir.FullName)
End If
uploadDir.MoveTo(targetDir.FullName)
End If
End If
Return RedirectToAction("Index")
Catch
Return View(vm)
End Try
End Function |