更新一对一关系EntityState.Modified .net

时间:2017-06-09 06:06:55

标签: sql wpf vb.net linq entity-framework-6

我有一个简单的模型Config和MyValue:

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

我想在更改后更新我的数据库中的配置: - TextBox中的名称文本(绑定ViewModel) - ComboBox中的MyValue对象(绑定ViewModel) - 具有约束力的其他值

Public Function SaveExececute() As String
   ObjConfig.Name = SomeString    'FAKE All is binded'
   ObjConfig.MyValue = SomeObject 'FAKE All is binded'
   ' ... '

Using ctx As New DbContext
     Try
        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
End Sub

IMAGE DEMO

在数据库上修改的所有值除了我的新MyValue与Id 2(我的sql数据库上的MyValue_MyValueId = 1),Disconnect方法无法识别新对象MyValue,如何附加和更新新的对象MyValue?< / p>

1 个答案:

答案 0 :(得分:0)

我对Vb.Net并不擅长,所以我会尝试(我使用C#)

您的ObjConfigctxUsing ctx As New DbContext Try Dim ObjConfig as New ObjConfig() = ctx.ObjConfigEntity.Find(ObjConfig.Id) ''Or seach by you data Dim MyValue as New MyValue() = ctx.MyValueEntity.Find(ObjConfig.MyValue.Id) ctx.Entry(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 的上下文不同,因为您在不同的情境中打开

要解决此问题,您应该执行以下操作:

using

总之,您无法使用using中加载的数据来更新# fc1 layer W_fc3 = weight_variable([512, 1000]) b_fc3 = bias_variable([1000]) output = tf.nn.relu(tf.matmul(h_fc2, W_fc3) + b_fc3) #print("output: {}".format(output.get_shape())) return output 中的开放背景,因为它们不同