Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing a small website in MVC but stuck in very beginning. I am confused about multiple things and approaches. Those are as following:-

1-What is a model and what does it contain? Does it contain or map only tables in the database or it is something else. Please provide an example of a simple model or
in a master detail scenario. I am asking it because we have navigational properties in entity framework. So how this can be shown if not using entity.

2-Every where I find entity framework as connectivity option. I want to know if this is the only option for connectivity.I mean can I use stored procedures(not in entity framework).

3-If I am using stored procedures,how will I define models.

4- Lastly, If I start with entity framework what should be used:

4a-Database first-Auto generated code,hard to modify
4b-Code first- Evey time regenerates db when changes are there in model

So you can see that there are lot of foolish questions but trust me,these are the answers I need at this point of time.
Thank You.
Posted

Let me answer your question more specifically and a bit well-described way. First of all, MVC framework[^], is not an approach of ASP.NET application building only. It is an architecture in software development, used for distributing the source code into three categories,


  1. Model -- I will explain it a little better below, for now they contain the data.
  2. View -- It is the actual HTML content of your website
  3. Controller -- The back-end logic of your application, including the handlers


Now coming to the Model part of the MVC. ASP.NET uses the same pattern for application development, Model-view-controller. A model is the class, that defines the structure of data in your application. It simply defines what members your object has, and a few other functions related to your data (such as getting enumerable for your objects; list).

Secondly, you do not need Entity framework to work with Models in ASP.NET MVC. A model is just the definition, it doesn't has to be coming from Entity framework, or a database even. You are not supposed to have a database to use a Model. I will attach an article of mine with this answer to support this statement.

You can use those stored procedures to execute and return the data, using that data you can fill your models. You will be using the Controller to execute the Sql commands to get the data from a stored procedure... And thus then fill in the Model. Just make sure you're having a member inside your model, which can hold a list. Please see the attached article; for more on this.

If you're not using Entity framework, then don't use it. Who's forcing you to use one? I will not answer this part.

This is the article of mine, that explains ASP.NET MVC for beginners and intermediates. It doesn't use Entity framework, because Entity framework has been explained well by the ASP.NET developers. That is why I tried using JSON (not SQL database) for the data source for my application. I guess that article is a perfect for your condition here. You should try it a try. It will clarify all of your concepts and confusions.

Understanding ASP.NET MVC using real world example, for beginners and intermediate[^]
 
Share this answer
 
Comments
Manvendra Singh 22-Mar-15 12:38pm    
Thank you Afzaal.Your article is very informative. Going through it and creating a sample.
So If I have two tables of master child relationship in database,I need to create/map all columns in model as properties including their relationship(pk,fk etc)?
Afzaal Ahmad Zeeshan 22-Mar-15 13:41pm    
Yes, you will create the model as if to reflect the table structures entirely. You are not required to have only one object in Model, you can create different objects (classes) in your Model set and use them as you want to use them.
Manvendra Singh 23-Mar-15 0:59am    
OK.Trying that out. Will be back if I get more questions. Thank You.
Manvendra Singh 23-Mar-15 1:02am    
Both of the solutions helped me a lot. So I can't mark one of them as accepted as that would be misleading. So if anyone wants the solution,go through both of the answers.
Afzaal Ahmad Zeeshan 23-Mar-15 1:05am    
Did you know? You can manually select both of the posts as answer. So, both would be shown as answers. :)
Quote:
What is model?


Model is class that maps to the Table.

Quote:
what does it contain?

It contains all application logic like business logic, validation logic, and data access logic.

Quote:
I want to know if this is the only option for connectivity


No it is not the only option .It is the one of the best option.
why it is best?
The same question raised when i start to learn MVC.Refer it.
Entity Framwork or LINQ for MVC ?[^]

Quote:
can I use stored procedures


Yes you can use Stored Procedures in MVC


Quote:
If I am using stored procedures,how will I define models.


Refer it.
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/async-and-stored-procedures-with-the-entity-framework-in-an-asp-net-mvc-application[^]


Quote:
If I start with entity framework what should be used:


Refer it
EF code first or Model First or DB First?[^]


I hope its help you.
 
Share this answer
 
Comments
Manvendra Singh 21-Mar-15 14:10pm    
Thank you very much King Fisher. Currently I am going through the links you provided. Will ask you back If I get more questions.
Thank you.
King Fisher 21-Mar-15 14:13pm    
Welcome buddy.
Afzaal Ahmad Zeeshan 21-Mar-15 17:41pm    
Actually ASP.NET MVC can run without Entity framework, or without having a database at the back-end. You can use any kind of data source, just use your controller to load the data into the Model; Entity framework also does the same. It loads the data from your database and fills your models for your view. For more, please see Solution 2 of mine, and the attached article in it.
King Fisher 22-Mar-15 12:21pm    
I didn't say you should use Entity Entity Framework.
King Fisher 24-Mar-15 1:24am    
Can you tell me the reason for the downvote?

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