CodeLearner
CodeLearner

Reputation: 439

MVC database table relationships explained

I was hoping someone would help explain some of MVC to me.
Say for example you have,

var orderDetails = new List<App_Orders>();

App_Orders is a table in the database that the application is using. So I say orderDetails is a List of type App_Orders?

In this example, say the user clicks on a link to open an existing Customer, on screen is listed all the Customers Orders. The code gets the Orders that are on screen...

List<App_Orders> ordersInSession;
.....
ordersInSession = (List<App_Orders>)Session["Orders" + id];

If my understanding is right of MVC and the way the tables in the database are joined, if a table has a relationship with App_Orders, it will be available when accessing ordersInSession. For example, App_Orders has a relationship with App_OrderItems and Ref_OrderType. An Order can have one or more Items and the Orders are of a particular Type. So when you type, ordersInSession. , intellisense will pop up with the list of properties of App_Orders, for example, OrderID, OrderDate, but will also have App_OrderItems and Ref_OrderType. Am I understanding that right? If the table has relationships, then those tables are also available? If I am, would you call those attributes?

Sorry I'm not even sure that made sense, but if anyone has any idea what I mean if they know of a tutorial that I can read to help me understand this part of MVC better.

Thanks in advance

Upvotes: 0

Views: 392

Answers (2)

Vincent67
Vincent67

Reputation: 11

ASP.NET MVC is a framework for building web applications, it has "nothing to do" with databases and is not providing facilities to manipulate them.

The idea is that the user asks a Controller to show data (Model) on a "page" (View). Nothing to do with databases.

What you want probably is to add some code in your MVC Controllers so they build a Model from objects in a database. For this your can use an ORM framework like Microsoft's Entity Framework, or lightweight/easy things like Dapper or PetaPoco.

Your MVC controllers can then load data from the database, construct a list of App_Orders and give that as a Model to the View to display.

Avoid storing things in the Session as much as you can, it's not meant to hold volumes of data and it's asking for trouble e.g. if the browser opens multiple tabs in the browser.

Upvotes: 1

Are you using any ORM framework? For Entity Framework you can read about relationship here

Upvotes: 0

Related Questions