Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ASP.NET MVC Model
Message
From
08/07/2013 11:01:42
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01577926
Message ID:
01577962
Views:
56
>>>>>I am learning how to create Model using EF (learning from various Pluralsight courses). So far what I understood is that the controller creates a model from EF and passes it to the View. And the examples I watched - so far - deal with only one table. But what if I need to use more that one table in the View? For example, a "main" table for the fields of the View and several tables used as look-ups? Does it mean that the controller then creates several models and passes to the View not one but several Models?
>>>>>
>>>>>TIA.
>>>>
>>>>You should add multiple tables in the EF model and define the relationships between them. If, as the classic example, you have Customer, Order and OrderDetail tables then the EF Customer entity will then have a collection of Orders and each Order will have a collection of OrderDetails.
>>>
>>>What if there is a table in DB/EF that has no relationship to the main table? E.g. Some Reference Code. But you want this table to be a pull-down of one of the fields of the, say, Order table. So that the user can - optionally - select/assign a certain code to the Order. How do you pass the collection of this ReferenceCode to the View?
>>
>>Tables in the EF designer don't *have* to have a relationship - Add the table in the designer as an independent entity. But in the example you give it seems to me that there is a relationship - the ReferenceCode in the Order table relates to an item in the table with the list of options ?
>
>The only thing is set in the database, as far as relationship, is Foreign Key Constraint for the field Reference in ReferenceCode table and the same field name (Reference) in the Orders table. And I don't know if this is the "relationship" that is needed for EF or there is something other than that.

If the relationship exists in the database then it's usually best to preserve that relationship in the model but you will still need to use something like the DropDownListFor helper to populate the list for the browser.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform