Skip to content

Instantly share code, notes, and snippets.

@dreadjr
Created September 12, 2012 17:51
Show Gist options
  • Save dreadjr/3708558 to your computer and use it in GitHub Desktop.
Save dreadjr/3708558 to your computer and use it in GitHub Desktop.
Entity Framework 5 Code First CustomEntityTypeConfiguration to force datetime2 usage on common inherited fields
public class CustomEntityTypeConfiguration<T> : EntityTypeConfiguration<T> where T : EntityBase
{
public CustomEntityTypeConfiguration()
{
this.Property(t => t.CreatedDate).HasColumnType("datetime2");
}
}
// usage
public class EntityBase {
public long Id {get;set;}
public DateTime CreatedDate {get;set;}
}
public class SomeEntity : EntityBase {
public string Name {get;set;}
}
public class SomeEntityConfiguration : CustomEntityTypeConfiguration<SomeEntity>
{
public SomeEntityConfiguration()
{
//configure key and properties
this.HasKey(c => c.Id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment