Link for all dot net and sql server video tutorial playlists
Link for slides, code samples and text version of the video
In Part 14, we discussed Conditional Mapping in entity framework with database first approach. Please watch Part 14 before proceeding. In this video we will discuss Conditional Mapping in entity framework with code first approach.
To implement Conditional Mapping with code first override OnModelCreating() method
using System.Data.Entity;
namespace Demo
{
public class EmployeeDBContext : DbContext
{
public DbSet[Employee] Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity[Employee]()
.Map(m =] m.Requires(“IsTerminated”)
.HasValue(false))
.Ignore(m =] m.IsTerminated);
base.OnModelCreating(modelBuilder);
}
}
}