Entity Framework EntityKey / Foreign Key problem

前端 未结 3 1187
借酒劲吻你
借酒劲吻你 2020-12-10 07:23

As the result of a form post, I\'m trying to save a new Brand record. In my view, Gender is a dropdown, returning an Integer, which is populated from ViewData(\"gender\")

3条回答
  •  半阙折子戏
    2020-12-10 07:43

    Ok heres what I've come up with instead of using updateModel. It appears to be problematic / potentially a security risk anyway with potentially additional fields being added in the post. A New instance of Gender can be instantiated via trygetObjectByKey. This is working so far. Any other suggestions on getting UpdateModel to update the Gender property object appreciated.

    Dim cID As Integer
    cID = CInt(Request.Form("Gender"))
    Dim key As New EntityKey(DB.DefaultContainerName() & ".Gender", "ID", cID)
    
    Dim objGenderDS As New Gender
    'Bring back the Gender object.
    If DB.TryGetObjectByKey(key, objGenderDS) Then
    Brand.GenderReference.EntityKey = key
    Brand.Gender = objGenderDS
    DB.AddToBrand(Brand)
    DB.SaveChanges()
    Else
    End If
    

提交回复
热议问题