Click here to Skip to main content
11,934,783 members (49,036 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked

Entity Framework Code First: Mapping to Existing Table in the Database

, 30 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip is about how to connect with existing table in the database.


This tip is about how to connect with an existing table in the database.


For learning Code First, you can refer to the following:

Using the Code

The database name is MyClass and it consists of only one table "ClassAll".

Below is ClassAll class with the same attributes and properties as of the table .

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace School.Data.Model
    public partial class ClassAll
        public int SchoolRollNo { get; set; }
        public int Standard { get; set; }
        public int ClassRollNo { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int Percentage { get; set; }
        public System.DateTime Date { get; set; }

The class ClassAllMap is used to map with the existing table in the database.

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace School.Data.Model.Mapping
    public class ClassAllMap : EntityTypeConfiguration<ClassAll>
        public ClassAllMap()
            // Primary Key
            this.HasKey(t => t.SchoolRollNo);

            // Properties
            this.Property(t => t.SchoolRollNo)

            this.Property(t => t.Name)

            this.Property(t => t.Gender)

            // Table & Column Mappings
            this.Property(t => t.SchoolRollNo).HasColumnName("SchoolRollNo");
            this.Property(t => t.Standard).HasColumnName("Standard");
            this.Property(t => t.ClassRollNo).HasColumnName("ClassRollNo");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.Gender).HasColumnName("Gender");
            this.Property(t => t.Percentage).HasColumnName("Percentage");
            this.Property(t => t.Date).HasColumnName("Date");

MyClassContext is the reference to the "MySchool" database. In this code, we are using the ClassAllMap declared above for configuration.

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using School.Data.Model.Mapping;

namespace School.Data.Model
    public partial class MyClassContext : DbContext
        static MyClassContext()

        public MyClassContext()
            : base("Name=MyClassContext")

        public DbSet<ClassAll> ClassAlls { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
            modelBuilder.Configurations.Add(new ClassAllMap()); 

If using SQL Server Express Edition, in web.config file, add the connection string as follows:

    <add name="MyClassContext" providerName="System.Data.SqlClient" 
       connectionString="Server=.\SQLEXPRESS;Database=MyClass;Trusted_Connection=true;" />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shubham Chakraborty
Software Developer (Junior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Assil10-Nov-15 11:52
professionalAssil10-Nov-15 11:52 
Questionhow about EF code first migration Pin
Mahdi Ghafoorian27-Apr-14 5:58
memberMahdi Ghafoorian27-Apr-14 5:58 
GeneralMy vote of 1 Pin
zeewon23-Mar-14 2:56
memberzeewon23-Mar-14 2:56 
GeneralMy vote of 1 Pin
Member 28141395-Feb-14 2:11
memberMember 28141395-Feb-14 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 1 Oct 2013
Article Copyright 2013 by Shubham Chakraborty
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid