Good afternoon all,
I have been tasked with learning MVC through online tutorials and implementing this knowledge on a new web application. So I am a little green when it comes to MVC.
One issue I haven't solved so far is how to bind objects when using the Entity Framework. Classes with basic data types are retrieved with no issues, but now I created a 'Survey' class which includes a property of class 'Outcome'. 'Outcome' is a reference table, containing ID and Description columns, and is being populated successfully. In the database, the EF created a foreign key named 'Outcome_ID'. But when the Data Access Layer (which inherits the DbContext) compiles the DBSet(of Survey), the Outcome field is remaining null, despite making sure that all have valid Foreign Keys in the database.
Survey Class:
Imports System.ComponentModel.DataAnnotations
Public Class Survey
<Key>
<Schema.DatabaseGenerated(Schema.DatabaseGeneratedOption.Identity)> Public Property ID As Integer
Public Property Created As DateTime
Public Property LastModified As DateTime
Public Property Owner As String
Public Property Outcome As Outcome
Public Property SurveyCountry As Integer
End Class
Data Access Layer:
Imports System.Data.Entity
Public Class DAL
Inherits DbContext
Public Property Users As DbSet(Of User)
Public Property Surveys As DbSet(Of Survey)
Public Property Outcomes As DbSet(Of Outcome)
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
modelBuilder.Entity(Of User).ToTable("User")
modelBuilder.Entity(Of Survey).ToTable("Survey")
modelBuilder.Entity(Of Outcome).ToTable("Outcomes")
MyBase.OnModelCreating(modelBuilder)
End Sub
End Class
Repository Class Function (this returns a list of surveys, each with a null 'Outcome'):
Public Function GetSurveyList() As List(Of Survey)
If IsNothing(data) Then data = New DAL()
Return data.Surveys.ToList()
End Function
I am convinced that I need to add some form of binding on the field, or an initialisation, or something. What am I missing? Thanks in advance for your help!
What I have tried:
Looked this up online, but could not find a solution.