Avançar para o conteúdo principal

Entity Framework

This post is about entity framework and how to use it to make, almost with a simple mouse click, the CRUD (create, read, update and delete) operations over a table.

Let's start by creating a web MVC project with Visual Studio 2015.







 Now we add a database with a table for books, for that we use the right mouse button in the Data Connections link over the Server Explorer window and then choose Add Connection...

 When the database is ready use the right mouse button again, like so.

These are the fields for the table.
When everything is ready just click the Update option and the following window will popup.

On this one just click the Update Database button, if you can't maybe you have some error back in the create table script.
 After the executation of the script you can check the new table in the database.
 If it's not showing try refreshing the tables, be aware that the refresh option only refreshes the section selected so make sure tables is selected before trying to refresh.


When the table is finally created let's go over the Solution Explorer and use the right mouse button to create a new item of type ADO .Net Entity Data Model. You should right click the folder of the models so VS saves the data model in that folder.
 
 If the option is not visible you must choose new item and then search ADO.
Define a name for the data model, or keep the one that's already there and then choose EF Designer from Database.
Click next and choose the data connection or the database.
And then you can say Yes so that the VS copies your database into the App_Data folder of your project.
Finally choose the table books of the database.
When you click finish the VS will go over your table and build a model.

Next, we must compile (build) our project, so that the model we created can be used.
The following step is simple, we create a controller with all the options we want. To do that use the right mouse button and click the controllers folder and choose add - controller.

In the window that pops click on MVC 5 controller with views, using Entity Framework.
Now indicate the data model, the data source and the name to the controller.
And then we wait.

In the end of this process we get a controller with code and views ready to go.





Comentários

Mensagens populares deste blogue

Let's make a car in Unity 3D

In this post we will make a simple car in Unity 3D. The Unity 3D physics engine is used in order to give the car a real behavior. This are the steps: [1] - Create a new Project

PacMan 3D

In this post we will be making a simple and classic game, the Pacman, with Unity 3D. Let's create a new a new project

Upgrading Windows 10 Home to Pro

 So I have been thinking about upgrading my Windows 10 Home Edition to the Pro version, but I always get to the point where it seems that I had to reinstall the entire SO and quit. After some investigating I have done it this way: - following this post  on the microsoft site I use one of the default keys for Windows 10 Pro and went to Settings > Update & Security > Activation > Change the product key; - next, Windows will activate the Pro functionalities and asks to restart; - now you have the Pro version but it's not activated, so you have to buy a Windows Pro Key. I went to UR cdkeys  and bought a key for less then €20; - and with the new key went to Change the product key and activated; - and it's done. Disclaimer : I have nothing to do with UR cdkeys so you can use any site to buy you cd key and your experience may vary from mine.