Introduction
In this blog we will see how to access sql server database with entity framework code first approach and later we will also look at how we can perform select data operation using complex type.
Step 1: Create asp.net web application
Employee.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace ComplexType_CFA_SelectApp
- {
- public class Employee
- {
- public Employee()
- {
- }
-
- public int Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public EmployeeDetails EmployeeDetails { get; set; }
- }
- }
EmployeeDetails.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.Linq;
- using System.Web;
-
- namespace ComplexType_CFA_SelectApp
- {
- [ComplexType]
- public class EmployeeDetails
- {
- public EmployeeDetails()
- {
- }
-
- public int Phone { get; set; }
- public int Age { get; set; }
- public string Email { get; set; }
- }
- }
Employeecontext.cs
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
-
- namespace ComplexType_CFA_SelectApp
- {
- public class EmployeeContext: DbContext
- {
- public EmployeeContext()
- : base("EmployeeConn")
- {
- Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());
- }
-
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
-
- modelBuilder.Entity<Employee>().HasKey(m => m.Id).Property(m => m.Id).IsRequired();
-
-
- modelBuilder.Entity<Employee>().Property(p => p.FirstName).IsRequired().HasMaxLength(50);
-
-
- modelBuilder.Entity<Employee>().Property(p => p.LastName).IsRequired().HasMaxLength(50);
-
-
- modelBuilder.ComplexType<EmployeeDetails>().Property(p => p.Age).IsRequired();
-
-
- modelBuilder.ComplexType<EmployeeDetails>().Property(p => p.Phone).IsRequired();
-
-
- modelBuilder.ComplexType<EmployeeDetails>().Property(p => p.Email).IsRequired();
- }
-
- public DbSet<Employee> Employees { get; set; }
- }
- }
Web.config
- <connectionStrings>
- <add name="EmployeeConn"
- connectionString="Data Source=WIN-B4KJ8JI75VF;Initial Catalog=EmployeeDB;Integrated Security=true"
- providerName="System.Data.SqlClient"/>
- </connectionStrings>
Webform1.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ComplexType_CFA_SelectApp.WebForm1" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- </div>
- </form>
- </body>
- </html>
Webform1.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace ComplexType_CFA_SelectApp
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- EmployeeContext entities = new EmployeeContext();
- protected void Page_Load(object sender, EventArgs e)
- {
- var data = from x in entities.Employees
- select new
- {
- EmpId = x.Id,
- Firstname = x.FirstName,
- Lastname = x.LastName,
- Age = x.EmployeeDetails.Age,
- Email = x.EmployeeDetails.Email,
- Phone= x.EmployeeDetails.Phone
- };
- GridView1.DataSource = data.ToList();
- GridView1.DataBind();
- }
- }
- }
Output of the application looks like this
Summary
In this blog we have seen how we can access sql server database with entity framework code first approach and how to perform select data operation using complex type. Happy coding!