Different way of mapping with EFCodeFirst

  • Home
  • Blog
  • Different way of mapping with EFCodeFirst

Recently I have been working Entity Framework Code First 6.0 and it’s awesome. I am in love with it. It has got great fluent API and we can do anything with that. It’s also promotes Convention over configuration which I love so far. I have seen people are more confused about how we can map table to class via Entity framework via code first. So this post is going to be in same series. In this post I’m going to write about different ways to mapping table to class. I have already written two post about in details.

Entity Framework code first and Inheritance–Table per hierarchy

Entity framework code first and inheritance- Table Per Type In this blog post I will give you tips and tricks to map class(type) with database table.

As you know all mapping logic belongs to OnModelCreatingMethod and you need to override method like following and write a logic there.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    base.OnModelCreating(modelBuilder);
}

Followings are few options for mapping class to table.

modelBuilder.Ignore(); 
modelBuilder.Entity().ToTable("Employee");
using System.ComponentModel.DataAnnotations.Schema;

namespace EntityCodeFirstInheritance
{
    [Table("Employee")]
    public class Employee 
    {
        public int  EmployeeId { get; set; }
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Designation { get; set; }
    }
}

This is very basic concepts of mapping in Entity Code First. We are going to explore some other type of complex mapping in next post related to entity code first.

Hope you like it. Stay tuned for more!!.