asp.net mvc - How to pass query results with FK relations(EF) to the View -
I do not understand this simple thing in ASP.Net MVC:
I have two tables :
Customer: Customer ID First Name Last Name Address ID Address Timeline Address: Address Id Street City I have my relationship (FK) in DB and use of the unit Let's try to framework My navigation property (FK) has been given the name and address1. I have created a repository:
Public interface ICustomertRepository {IQueryable & lt; Customers & gt; FindAllCustomers (); } Public Category Customer Repository: MvcApplication2.Models.ICustomerRepository {Public IQueryable & lt; Customers & gt; FindAllCustomers () {return db.Customers; }} And in my home controller this is:
Public Action Index () {var query = customerRepository.FindAllCustomers (); See Return (query); } But how can I use values in my view? I can see the item. Ed. Sir / item.edress1 With Intellisense with the model. But when I try, I'll get a tap reference expression:
& Lt; / TD & gt; & Lt; TD & gt; & Lt;% = HTML.Encode (item.Adresses1.Street)%> & Lt; / TD & gt; & Lt; / TR & gt; & Lt;%}% & gt; I have lost completely I tried to search everywhere but without any success.
EF does not automatically load lazy.
You need to manually load related objects (not possible for multiple results set such as your query), or direct EF to include the desired properties for the entire query:
public IQueryable & LT; Customers & gt; FindAllCustomers () {returns db.Customers.Include ("Adresses"). Include ("Adresses1"); } You can compare how to do this between EF and LinuxSQL (if you are used to it). This will at least show you the EF syntax for it.
Comments
Post a Comment