Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Visual Basic
Tip/Trick

EF Code First Fluent Configuration of Abstract Entities

Rate me:
Please Sign up or sign in to vote.
4.20/5 (5 votes)
10 Sep 2014CPOL 13.1K   4   2
How to configure columns of abstract entities.

Introduction

When I was building my set of POCOs, I saw commonality in some fields and created abstract classes to represent these fields. The main entities then inherited from these, however when I came to configure the EF more exactly, I found it wasn't obvious how to do it.

Using the Code

So if you have an entity defined like:

C#
public class UnitType : ListValidRange
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Display(Name = "Possible Review Types")]
    public virtual ICollection<ReviewType> ReviewTypes { get; set; }
}

Where ListValidRange is:

C#
public abstract class ListValidRange : AuditInfo, IValidatableObject
{
    [DataType(DataType.Date)]
    public DateTime ValidFrom { get; set; }
    [DataType(DataType.Date)]
    public DateTime ValidTo { get; set; }

    #region IValidatableObject Members
    public System.Collections.Generic.IEnumerable<ValidationResult> Validate
        (ValidationContext validationContext)
    {
        if (ValidFrom > ValidTo || ValidTo == ValidFrom)
             yield return new ValidationResult(
                 "ValidFrom must be different from, and occur earlier than, ValidTo."
        //if both fields are mentioned, then you get duplicate messages
        //well no you get the same message for both fields
        //but once you receive it from the DataServices they've doubled!
                 , new[] { "ValidFrom" });
    }
    #endregion
}

Now to the fluent configuration, the simple (and well documented way) is to configure ValidFrom/ValidTo on UnitType, but that's time consuming if a lot of entities have those fields.

So to configure ListValidRange, you need to use the Types() collection of the modelBuilder in your OnModelCreating method, like so:

C#
modelBuilder
    .Types<ListValidRange>()
    .Configure(p => p.Property(e => e.ValidFrom)
                     .HasColumnType("date"));

So it's slightly different from the examples you've probably already been looking at, but I hope it's easy to see how it works and can be expanded to suit your needs.

License

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


Written By
Software Developer Bank Of Ireland
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
darkliahos11-Sep-14 0:04
darkliahos11-Sep-14 0:04 
GeneralMy vote of 4 Pin
Duncan Edwards Jones10-Sep-14 23:22
professionalDuncan Edwards Jones10-Sep-14 23:22 

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.