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

Single Page App with C# WPF/XAML

 In this post we are going to create a single page app. The app will have multiple pages that get rendered in the main window. We will be using Visual Studio, C#, WPF and XAML. Let's start by creating a new project in Visual Studio of this type: Next, in the MainWindow, we define the interface structure. On the left side we place a menu and on the right side a DockPanel with a Frame in it. The Frame is the element that is used to render de pages content. Now let's add the new pages. In this example I will add two pages. Click in the Solution Explorer with the mouse right button, then choose Add and Page. The project looks like this. The app content goes on the recently create pages. Because this is just an example I will just change the background color and add a small text. Page1 Page2 Finally the code. Back to the MainWindow we need to create the click events on the menu items. So, in the MenuItem line add the click event and pick New Event Handler. If that option doesn't