如何在插入行时手动插入主键 - EF

时间:2015-04-16 06:00:31

标签: asp.net-mvc entity-framework model-view-controller entity-framework-4

我在Visual Studio 2012中使用MVC 4.我想在创建行时插入主键。我的表格如下:

<Table("StudentRec")> _
Public Class StudentRec
   <Key()> _
  Public Property ErNo as Integer
  Public Property Name as String
  Public Property Branch as String
  Public Property Course as String
End Class

<Table("Attendance")> _
Public Class Attendance
   <Key()> _
  Public Property ErNo as Integer
  Public Property Present as Integer
  Public Property Leaves as Integer
End Class

我的行动控制器是:

<HttpPost()> _
<AllowAnonymous()> _
<ValidateAntiForgeryToken()> _
    Function CreateUser(ByVal Modl As StudentRec) As ActionResult
        Try
            Using dbb As New UsersContext
                Dim Erno As Integer = dbb.StudentRec_.Add(Modl).ErNo
                dbb.SaveChanges()

                Dim Attnd As New Attendance
                Attnd.ErNo = Erno  ' Not Working, Erno is automatically Set '
                Attnd.Leaves = 0
                Attnd.Present = 0

                dbb.Attendance_.Add(Attnd)
                dbb.SaveChanges()

            End Using
            RedirectToAction("Index", "Home")
        Catch ex As Exception
            TempData("Errs") = ex.Message
            Return View(Modl)
        End Try
        Return View(Modl)
    End Function

如何手动将主键设置为表出勤,关于表StudentRec的主键?

0 个答案:

没有答案