Posts

Showing posts with the label Entity Framework

Donate

Repeater Web Server Control In ASP.NET Web Forms With Bootstrap And Entity Framework

Image
Happy New Year! This post will illustrate on how to apply Bootstrap styling to a Repeater control given that the template used will be a Table element. To begin with, create an ASP.NET WebForm project. This application will retrieve Employee information from Northwinds database. Add an ADO.NET Entity Framework 6.0 to your project which will hook up with the Employee table. Add a WebForm page to your solution then drag a Repeater control inside the div tag below the form tag. Make sure to reference the bootstrap file in your head tag. The Repeater control will utilize the table element as it's template for displaying employee information. For the record, the employee photo will be shown on the left column, while the personal details are presented on the right column. The table also made use of Bootstrap's table css classes. <head runat= "server" > <title></title> <link href= "Content/bootstrap.css" rel= "stylesheet&q

DataList In ASP.NET MVC With Paging Using PagedList And Bootstrap

Image
Here's an example ASP.NET MVC application that shows information in a DataList layout with pagination using PagedList Pager. The project also integrates Bootstrap for enhancing the UI and display the images from AdventureWorks Products table through an ImageHandler class. The significant files used in this project are: 1. PagingModel.cs/IndexModel.cs - These classes are responsible for defining the page number, size of page, and PagedList property that stores the information that are paged accordingly to it's size. public class PagingModel { public PagingModel () { Size = 24 ; Page = 1 ; } public int Page { get ; set ; } public int Size { get ; set ; } } public class IndexModel { public PagingModel PageModel { get ; set ; } public IndexModel (PagingModel pageModel) { Products = new PagedList<ProductViewModel>( new List<ProductViewModel>(), 1 , pageModel.Size); PageModel = pageModel; } public IPagedList<ProductViewModel>

Task Management System With Entity Framework And ASP.NET MVC

Image
Hello, Here's a simple Task Management System with Entity Framework 6 taken from Udemy's Asp.Net MVC With Entity Framework From Scratch video tutorial. The output of the project should be in ASP.NET Webform but I chose to upload a sample in ASP.NET MVC which is intended for ASP.NET MVC developers. The entire source code can be downloaded here: Task Management System MVC . The project includes the stored procedure necessary to display the data through the grid. As for the database table, it is included in the tutorial series through a PDF file. Output Cheers!

Call Stored Procedures From Entity Framework In ASP.NET MVC

Image
Good day! Here's an ASP.NET MVC example of a CRUD(Create/Update/Delete) application using stored procedures and Entity Framework 6.First, you need to perform steps 1-3 from this link Call Stored Procedures from Entity Framework 6 in C# (Part 1) . For step 3, instead of creating a console application use ASP.NET MVC Empty project. Once done, the code for the controller and views are shown below: CustomersController private CustomerEntities db = new CustomerEntities(); // GET: Customers public ActionResult Index() { return View(db.Database.SqlQuery<Customer>( "GetAllCustomers" ).ToList()); } // GET: Customers/Create public ActionResult Create() { return View(); } // POST: Customers/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "CompanyName,ContactName,Address,Country,Phone" )] Customer customer) { if (ModelState.IsValid) { db.Database.ExecuteSqlCommand( "EXEC dbo.InsertCustomer @Compan

Call Stored Procedures from Entity Framework 6 In C# (Part 2)

Hello, In this second part of the series on how to call stored procedures from Entity Framework 6, I will demonstrate on executing them using context.Database methods. To accomplish this, you need to perform steps 1 through 3 from this article Call Stored Procedures from Entity Framework 6 in C#. If you're done, the codes for the CRUD(Create, Update and Delete) functionalities are presented below. private static CustomerEntities ce = new CustomerEntities(); private static void InsertCustomer() { try { var result = ce.Database.ExecuteSqlCommand( "EXEC dbo.InsertCustomer @CompanyName,@ContactName,@Address,@Country,@Phone" , new SqlParameter( "CompanyName" , "TNT Bookstore" ), new SqlParameter( "ContactName" , "Mr T." ), new SqlParameter( "Address" , "Lincoln Village" ), new SqlParameter( "Country" , "UK" ), new SqlParameter( "Phone" , "42333&qu

Call Stored Procedures from Entity Framework 6 In C# (Part 1)

Image
Hello, Here's a tutorial on how to call stored procedures from Entity Framework 6.0 through the context object using the stored procedure name. In the second part of the series, I'll demonstrate how to call the stored procedures using methods like ExecuteSqlCommand() and SqlQuery() from context.Database class. To start with here are the steps to complete this example. Step 1 Add a Customers table in your database with fields. => CustomerID (int and identity set to true) => CompanyName(nvarchar) => ContactName(nvarchar) => Address(nvarchar) => Country(nvarchar) => Phone(nvarchar) Step 2 Create stored procedures that will perform insert, update, delete and get all records operations. Insert ALTER Procedure [dbo].[InsertCustomer]( @ CompanyName nvarchar( 40 ), @ ContactName nvarchar( 30 ), @ Address nvarchar( 60 ), @ Country nvarchar( 15 ), @ Phone nvarchar( 24 )) As Begin Insert Into dbo.Customers (CompanyName, ContactName, [Address

Navigation Properties In Entity Framework Using Database First Approach

Image
Good day! Here's a simple step by step tutorial on exploring the Navigation Properties of EF using the DB approach. According to MSDN , Navigation properties provide a way to navigate an association between two entity types. Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). You may also choose to have one-way navigation, in which case you define the navigation property on only one of the types that participates in the relationship and not on both. Given the description, this example demonstrates the concept using two tables Employees and Dependents wherein you search for a particular employee and you can access the related dependents of that employee. To start with, just perform the steps given below. Step 1. Cre

Exclude Property Mapping In Entity Framework Code First Approach

Hi all, Given a database table such as User with fields Id, UserName and Password that will be mapped to a class using Entity Framework called User with an additional field of ConfirmPassword. public class User { public int Id { get ; set ; } public string UserName { get ; set ; } public string Password { get ; set ; } public string ConfirmPassword { get ; set ; } } I would like to exclude the ConfirmPassword field from being mapped to the User table. So, after doing some searching, the solution is to decorate the property with [NotMapped] attribute and make sure to reference the DataAnnotations namespace ( System.ComponentModel.DataAnnotations.Schema ). using System.ComponentModel.DataAnnotations.Schema; public class User : IEntity { public int Id { get ; set ; } public string UserName { get ; set ; } public string Password { get ; set ; } [NotMapped] public string ConfirmPassword { get ; set ; } }

LINQ to Entities does not recognize the method 'System.String ToString()

When working with Dropdownlists in ASP.NET MVC, I encountered an error as stated in the title of this post when casting an int variable through LINQ Select() statement. List<SelectListItem> itemCountries = new List<SelectListItem>(); itemCountries.AddRange(( from country in _context.CascadeCountries select country) .Select(x => new SelectListItem { Value = x.CountryID.ToString(), //error here... Text = x.CountryName }).ToList()); After doing some research, I found out that the statement returned by the Select statement above is IQueryable and the itemCountries variable is an IEnumerable . So, the fix for this issue is to cast the LINQ statement with AsEnumerable() and then followed by the Select() statement which sets the values for the SelectListItems properties. List<SelectListItem> itemCountries = new List<SelectListItem>(); itemCountries.AddRange(( from country in _context.Casca

ASP.NET FormView CRUD With Entity Framework

Image
Most of the examples on FormView Web Server control use DataSource wizard controls such as SqlDatasource or ObjectDataSource when assigning value to the FormView's DataSource property. However, using those controls have drawbacks such as maintainability. I also found samples out there using ADO.NET. Enough with the chit-chat and let's proceed with coding. I'll post the create table statement, code behind and the aspx markup. SQL Code: USE [Books] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[BookDetails]( [BookSerialNo] [int] IDENTITY (1,1) NOT NULL , [BookISBN] [ nchar ](15) NULL , [BookTitle] [varchar](120) NULL , [BookAuthor] [varchar](60) NULL , [BookPublisher] [varchar](50) NULL , [BookCategory] [varchar](20) NULL , PRIMARY KEY CLUSTERED ( [BookSerialNo] ASC ) WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_

Donate