The default database
You have created your first project ASP.NET MVC. You can run it through Visual Studio. The site is fully functional and you can register a new user and log in. If you can register and access means “below” the site is already a database. To know where the database and which tables are present for user management, open in Visual Studio, SQL Server Object Explorer and you will see your database under
LocalDB – DefaultConnection
DefaultConnection is the name of the connection string used by your application to connect to the database. If you list the contents of Tables, you see the tables that manage users. If you have created the site using the Framework 4.5.1 or 4.5.2 these tables are 6.
Our SQL Server database
We do not want to work on the default database, but on a SQL Server database on our local machine or network. Create a SQL Server database somewhere. Now we need to connect our website to this database. Open the web.config file and look for the connectionStrings tag. Replace the connection string with a valid connection to your SQL Server database (you will find the connection strings on the Internet). Let the tag name to “DefaultConnection”. Launch the application through Visual Studio.
This is a new database then the user you created before the default database, does not exist here. You must re-register and, at this point, you need to log in. If you now check the contents of your SQL Server database (with Visual Studio SQL Server Object Explorer or with SQL Server Management Studio) you will see that we are also here 6 tables that allow the user management. Created by the site itself.
In your project, in fact, you can find the model tables that allow the user management (in the Models directory), and your site, seeing that the database did not exist any tables, has created them for you. Very comfortable.
From here on, you will create your models, that is, the containers of your data and the site will always attempt to create the tables related to you. This is not an easy process because it does not allow you errors or second thoughts as you create your model. It ‘better to say to the site not to stay from now on to check the consistency of the model – table: you will build your hand models and then your tables of SQL Server. To communicate this to the site, open the file Global.asax.cs, and in Application_Start () function, add this line:
Database.SetInitializer <YourSite.Models.ApplicationDbContext> (null);
Where ApplicationDbContext is the name of the class that connects to the database using the DefaultConnection string. Find this class in the project.
nb. If you decide to work with this technology, whenever you have a doubt or need more details, just go to your favorite search engine and type in your search (with words in English), preceded by these simple words ASP.NET MVC. You will find all the answers.
When we talk about ASP.NET MVC, we mean, in general, the creation of sites with Visual Studio, using C # language and setting the development according to the programming rules MVC.
MVC stands for Model – View – Controller. In practice any old web page is now built using 3 different files; or, the 3 files together make up the page. Two of these files are pure C # code and have the extension .cs, the third is html code and .cshtml extension. In each of the three files is a piece of code. Program in this way will allow you to be more rapid development. For now, you have to trust me.
Let’s take an example. If you look with the browser http://www.miodominio.com/Products/Index page, the site will look for a file called ProductsController.cs (the first of our 3). In ProductsController.cs file exists, at least, the ProductsController class. Within this class, in this file, there is a function called Index. At this point the controller does 2 things: search the site for a file named Index.cshtml. This file (this is the View) contains our html. In some cases the page of the site there are only static phrases. If it is expected that this page contains dynamic data, the controller creates a whole representation of the data from a third file can have any name, for example Item.cs (the Model) and delivers it to Index.cshtml file there finally appears on the browser. Explanation long but it all works in a moment.
From the explanation you understand that there are strict rules about file names: there is then imposed a way of organizing our files, a way to go always the same that will make our maintainable code, ordered and readable by our fellow programmers.
Now open Visual Studio and create the first site in ASP.NET MVC. Find lots of guides on the web but you can also try it by yourself. We analyze the structure of folders that make up the site and see what we can do. In the site you will find surely these three folders:
The controller folder contains Controller file. You can give any name to these files in SomethingController.cs form. Inside the folder, you can create as many subfolders as you want and with any name to reorder better Controller files you create.
The Model Model folder contains files. You can give any name to these files and create as many subfolders as you want.
The Folder View contains the View. Inside this folder must exist for each controller, a subfolder called exactly as the Controller (without Controller.cs). In our examples, we will have a “Products” folder and a “Something” folder. Inside this folder there will be many .cshtml files as there are functions in the Controller file or, rather, the controller functions that you want to be used outside. They must be called exactly as the function. In our example we have:
The beauty of this whole management is that Visual Studio does it for you automatically and in a while we will see how.