Posts

Showing posts with the label Entity Framework

ASP.NET MVC 5 From Scratch : Create Entity Data Model With Entity Framework

Image
In our previous blogs we've created an ASP.NET MVC from scratch.  In this blog we are going to use Entity Framework as the ORM (Object Relational Mapping) as a conduit to our database, so that we can query our data as an object.  An ORM as the name implies maps database tables, views, and stored procedures as objects in a programming language so that developers can work with the data as objects. Step-by-Step Instructions: 1.  First we need to add the Entity Framework 6.1.3 to our ASP.NET MVC, we accomplish by right-click on "References" then select "Manage NuGet Packages" 2. On the search box type in "EntityFramework" no spaces, in the search result click on the "Install" button next to the "EntityFramework" package. 3.  Click "I Accept" on the "Licence Acceptance" screen 4. Click "Close" 5.  Once the "EntityFramework" package has been added you will see a green checkmark next to the "E...

Entity Framework (Database First) Part 4: Using the LINQ and Projection To SELECT Columns From Entities

Image
In the last blog we just selected the Products entities from the NorthwindEntities with this Linq Query. var query = from prod in nwctx.Products select prod; It gets the job done but, the GridView displays the CategoryID and SupplierID as integers, which is not very useful to your users. Plus, we don't want to display the ProductID. In this blog we are going to refine the LINQ query so that the GridView only display the columns that we want to display with Projection and Anonymous Types.  Projection is to transform an object into a new form with only the properties that you want in the new form.  In our case we want to transform the Products DataSet to a new form that only returns the data that we want. The resulting GridView after projection is looks like this. Notice that the ID column is hidden and the Category and Supplier shows the actual Category name and the Supplier name. Here are the steps: 1.  Open up the project that you've completed on the last blog....

Entity Framework (Database First) Part 3: Using the Entity Framework Objects In ASP.NET Project

Image
This is part three of our series on Entity Framework. In the last blog we went over how to create an Entity Framework model with the Northwind database. Now we are going to use that model in our ASP.NET by binding the Entity objects that have created to a GridView in our "Northwind" ASP.NET project. Usually we would put the Entity Framework model in a class library project and use it as our data access layer, but for simplicity I've decided to put in the same project as the ASP.NET pages. Below are the directions on how to use the Entity objects in our web pages. 1. Create "Default.aspx" page in the "Northwind" web project. 2. Add a GridView control to the page. 3. In the "Default.aspx.cs" file add the following using statement using Northwind.Models; Northwind.Models is the namespace of models that we just created. 4. Then in the Page_Load method write the following code. protected void Page_Load(object sender, EventArgs e) ...

Entity Framework (Database First) Part 2: Creating Entity Model From an Existing Database Entity Framework 6.1.1

Image
This is part two of our series on Entity Framework, if you would like to catch up with what we did on on part one , feel free to go over the lesson so that you can follow along. In the last part we installed Entity Framework 6.1.1 with NuGet package management tool in Visual Studio.  In this lesson we will learn to create an Entity Model using the Northwind database.  Follow the steps below. Add a new folder call "Models" in the "Northwind" database             2. Right-click on the "Models" folder and select "Add", the select "New Item"   3.   Select "Data" on the left tree menu under C#, then in the main menu select "ADO.NET Entity Model".  In the "Name" field type "NorthwindModel".  Click "Add" when you are done. 4.  On the "Entity Data Model Wizard" screen select "EF Designer from database" and then select "Next" 5. Click on the "New Connection...

Entity Framework Part 1: Installing Entity Framework 6.1.1 With NuGet

Image
In this blog I will show how to install Entity Framework 6.1.1 with NuGet in Visual Studio 2013 1.  Create a project call "Northwind" 2.  Right click on the solution that the project resides in, then select "Manage NuGet Packages for Solution...." 3.  The "Manage NuGet Packages" window is displayed 4.  In left hand side select "Online", and then select "nuget.org" 5.  In the "Search Online" textbox type in the word "EntityFramework", this will search for the latest version of EntityFramework available 6.  Click on the "Install" button 7.  Select the project you want install EntityFramework to be installed in, then click on "OK" 8. Click "OK" to accept the terms and conditions 9.  When the Entity Framework is installed you will see a check mark next to package 10.  Now you will see the the project has references to the Entity Framework DLLs Blogs in the Entity Framework Series: Installing...