BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
This blog explains the steps you'll need to follow to create a simple one page website in ASP.NET MVC. The aim of the blog is not to act as a tutorial, but to help people choose between ASP.NET MVC and classic ASP.NET.
Posted by Andy Brown on 16 June 2014
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Creating an entity model in MVC
There are three ways to access data using MVC, depending on whether you start with your code, your database or a pretty diagram of your data model. They are called:
|Code first||You create your classes (including their properties and methods), and ASP.NET generates a SQL Server database from this code. All very clever!|
|Model first||You create your model of how you want your database to look, and from this ASP.NET generates the database.|
|Database first||This is the approach we'll be taking - start with your SQL Server table, and from this generate a model and the code required.|
Here's how to generate an entity model. First, choose to add one:
Right-click on the Models folder in Solution Explorer, and choose to add a model.
Choose to create a new item:
Choose this option.
Find and create an ADO.NET entity data model:
Choose to add an entity data model (you'll also need to specify a name for this).
Our model will be generated from an existing database:
Choose to generate the model from an existing database.
You'll now need to go through the usual rigmarole of creating a connection (not shown here):
Choose to create a connection to your SQL Server database.
Having done this, go to the next stage of the wizard to generate the model (we've called our entity MoviesEntities):
Choose Next to continue the wizard.
Choose which tables you want to include in your model then choose to finish the wizard:
Here we've gone for a single table - tblFilm - and have chosen not to play about with the table and column names.
The result is a model which represents the underlying table and its columns.
You can rename the table and its columns at this point without affecting the underlying database table and columns.
Save and close the model diagram window and you will see that Visual Studio has created class for you:
My advice is not to bother looking at these classes. For one thing, they're quite complicated; but for another, .NET will recreate them whenever you regenerate your model.
We can now use this entity model to create a view model, as shown in the next step.