Entity Framework 6 Refresher: Mapping Files

1 minute read

Entity Framework can determine a lot of things about your model on it’s own, but at some point you need to provide some information about your classes that it has no way of knowing. There are three ways to do this: Attributes, via code in the DbContext itself and in separate mapping files.

Using attributes, we can decorate the class with metadata.

[Table("HobbyHorse")]
public class Hobby
{
    [Key]
    public int HobbyId { get; set; }

    [Required]
    [MaxLength(25)]
    public string HobbyName { get; set; }
}

This shows that we use a table name of “HobbyHorse”, that HobbyId is the primary key and that HobbyName is required and can be a maximum of 25 characters. This creates the following migration:

public override void Up()
{
    CreateTable(
        "dbo.HobbyHorse",
        c => new
            {
                HobbyId = c.Int(nullable: false, identity: true),
                HobbyName = c.String(nullable: false, maxLength: 25),
            })
        .PrimaryKey(t => t.HobbyId);
}

We can also configure the Hobby class in the DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Hobby>()
        .ToTable("HobbyHorse")
        .HasKey(x => x.HobbyId)
        .Property(x => x.HobbyName).IsRequired().HasMaxLength(25);

    base.OnModelCreating(modelBuilder);
}

The migration created from this is identical to the migration created when using attributes.

Finally (and preferably, IMO), we can configure Hobby in it’s own mapping file.

public class HobbyMap : EntityTypeConfiguration<Hobby>
{
    public HobbyMap()
    {
        HasKey(x => x.HobbyId);
        Property(x => x.HobbyName).IsRequired().HasMaxLength(25);
    }
}

This mapping file needs to be wired up in the DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new HobbyMap());
    base.OnModelCreating(modelBuilder);
}

Again, this creates an identical migration.

Leave a Comment