Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 11-Jul-17 21:15pm
Comments
Richard Deeming 11-Jul-17 10:59am    
Try including the navigation property:
Return data.Surveys.Include(Function (s) s.Outcome).ToList()

Entity Framework Loading Related Entities[^]
mgoad99 11-Jul-17 18:32pm    
I'm unclear if you have an MVC question or EF question. Either way, I do think your approach may be off.

I recommend the series of tutorials on MVC from Microsoft. For me, having an example is a great way to figure out how to get something working.

This is a link to a specific tutorial, but there are many more on this site.

https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/

Maybe looking thru the tutorial may help point you in the right direction.

1 solution

Thanks for your replies all! I managed to make it work in two different ways:

I tried Richard's solution, but did not work. But with a little tweaking it did:
Return data.Surveys.Include("Outcome").ToList()

Then I read this article: Lazy, Eager, and Explicit Loading of Related Data and realised that if I made the property 'Overridable', it worked. This is VB.NET's keyword for C#'s 'virtual'. This solution should create a little less overhead on the database than the first solution, if I understood correctly.
Public Overridable Property Outcome As Outcome

Thanks,

Charles.
 
Share this answer
 
Comments
Richard Deeming 12-Jul-17 7:51am    
Making the property overridable will enable lazy-loading. That will potentially increase the load on the server.

It's known as the "SELECT N+1" problem, because instead of a single query to load the N records and the related entity, you have one query to load the records, and another N queries to load the related entity.

If you know you're always going to need the related entity, using Include will almost always yield better performance.

Entity Framework Performance and What You Can Do About It[^]

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