Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello , i have a simple model Config and MyValue
VB
public Class Config
   Public Property ConfigId as Integer
   Public Property Name As String
   Public Property MyValue as MyValue
   Public Property ...
end class

Public Class MyValue
   Public Property MyValueId as Integer
   Public Property Value as Decimal
   Public Property Info As String
   Public Property ...
end class


I want to update my Config in database after changing:
- Name Text in TextBox (binding ViewModel)
- MyValue object in ComboBox (binding ViewModel)
- other values with binding

VB
Public Function SaveExececute() As String
   ObjConfig.Name = StrName     ' a simple string
   ObjConfig.MyValue = ObjValue ' SelectedItem from combo 
   ' other values here

Using ctx As New DbContext
     Try
        ctx.Entry(ObjConfig).State = Entity.EntityState.Modified
        ctx.SaveChanges()
        Return "UPDATED"
     Catch ex As Exception
         Return ex.Message
     End Try
End Using
End Sub


What I have tried:

all the values as been modified on database except my new MyValue ID 2 (MyValue_MyValueId = 1), the Disconnect Method do not recognize the new object MyValue, how to Attach and update the new Object MyValue ?

i Try to add this code but not working
VB
Using ctx As New LgmModel
           Try
               ' THIS NEW LINE
               ctx.Entry(ObjConfig.MyValue).State = Entity.EntityState.Modified
               ctx.Entry(ObjConfig).State = Entity.EntityState.Modified
               ctx.SaveChanges()
               Return "UPDATED"
           Catch ex As Exception
               Return ex.Message
           End Try
       End Using
Posted
Updated 8-Jun-17 2:37am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900