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

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...

Saving pictures to an Access database

What is the best method for storing images in a server? That's a discussion that we will never see the end. In my opinion, has in everything in life, it depends. But in this post we will not contribute to that dilemma. The objective of this post is to show how to store the pictures in an Access database. We start by creating a new project in VS 2013. Next, we create a class that will manage the database. In this class we will need a reference to a DLL that is responsible for creating the database file. In the Reference Manager with search in the COM section.    Back in the class with add a namespace: using ADOX; Now let's add three properties to the class: the path to the database file, a connection string and an object to make the connection with the database. class BaseDados     {         string caminhoBD;         string strLigacao;         OleDbConnection ...

Unity3D Coroutines

Coroutines are functions that maintain status while returning code execution to the method that called them. They are useful for the development of games to allow the execution of certain code over several frames. For example to animate the camera of the game we could execute the following code: for(int i=0;i<100;i++){     transform.position += new Vector3(1, 0,0); } If this code is within a function that is executed normally it is not possible to see the camera moving along the X axis since the game scenario update only occurs at the end of the cycle and not during the execution of this. In order to see the movement of the camera, it is necessary to perform an iteration of the cycle and then update the scene (rendering) and then continue the cycle with one more iteration and refresh the scene again, in this case what's needed is to interrupt the execution and to resume without losing the state, for this we can implement a coroutine. A coroutine is just an IEn...