0

I'm just trying the ASP.Net Entity framework its the first time I've tried to use an ORM framework so bear with me If I'm barking up the wrong tree.

To simplify the problem I've got the following 2 tables

Calendar

CalendarID UserID Date EventName

Users

UserId Username

I've added them both to my Entity Framework model and its established the link between the tables. I'm able to then display a list of Calendars from my MVC view, by using something like

  • <%= calendarEntry.DateAdded%>
  • However if I then try to use

  • ><%= calendarEntry.Users.Username%> : <%= calendarEntry.DataAdded%>

    It falls over on the call to calendarEntry.Users as it says it is null. Why is the entity framework not pulling through the use details? Do I need to change something in my model designer?

    If it helps the code in the MVC controller that sends the data to the view is like this

    var Entities = new UnityGamersEntities(); return View(Entities.Calendar);

    Really hope that makes sense.

    3 Answers 3

    1

    for anyone interested I solved this by using the following code

            UnityGamersEntities db2 = new UnityGamersEntities();
            ObjectQuery<GameCalendar> gc = db2.GameCalendar.Include("GameTitles");
    

    There seems to be a real lack of tutorials for the entity framework, unless you only ever want to work with single tables I found it really hard to find the information I need.

    hopefully this will change in coming months.

    Sign up to request clarification or add additional context in comments.

    1 Comment

    Yes, this annoys me completely I'd moved to EF from L2S due to it's better support of lookup tables, and then found that I lost my strong typing, due to having to declare all sub tables in strings.
    1

    Gav,

    There is another way you can get Linq to Entities to populate the Users Table.

    Every Foreign Key and Collection has a "Load" Method on it which will go off to the database and, in your case, fetch the list of users linked to the current calendar and populate the collection. It's kind of like lazy loading.

    If you were to add this line it should work:

    <!-- Load up Users unless it's already Loaded -->
    <% if(!calendarEntry.Users.IsLoaded) calendarEntry.Users.Load(); %>
    
    <!-- Your Line -->
    <%= calendarEntry.Users.Username%> : <%= calendarEntry.DataAdded %>
    

    Hope it helps.

    PS - I hear you on the lack of documenation

    Crafty

    Comments

    0

    You need to tell the entity framework to load the Users for the Calendar.

    You can do this VIA the LINQ query, simply:

    Calendar cal = (from c in Calendar
        where c.CalendarID.Equals(input)
        select new 
        {
            c,
            c.Users
        }).FirstOrDefault().c;
    

    Which says, load the calendar and all its users.

    EDIT: There are probably other ways to load the users, this is just the LINQ way.

    2 Comments

    Hi thanks for the advice, I've been playing with the code you posted and it works fine for returning one record. If however I change FirstOfDefault to ToList it just says cant convert annonymous type to Generic.List<GameCalendar>. Am I going about returning a list in the wrong way? Thanks again
    That's right because you're returning a list of many objects. Try this: var data = (from c in Calendar where c.CalendarID.Equals(input) select new { c, c.Users } And inspect data to see what the data structure looks like.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.