Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pluggable conventions in EF 5?

Was Pluggable conventions removed from the EF 5 release, i saw it in the pre releases? Looking for a way to force ef code use datetime2 by convention, so don't need to explicit map each column of every entity.

like image 695
dre Avatar asked Feb 03 '26 01:02

dre


1 Answers

Came across a similar issue. I have a generic repository that has a BaseEntity similar to yours.

public abstract class BaseEntity
{
    public int Id { get; set; }
    public DateTime Created { get; set; }
    public DateTime Updated { get; set; } 
}

and then each entity is derived from the base.

public class MyEntity: BaseEntity
{
    public string Name{ get; set; }
    public int prop1 { get; set; }
    public int prop2 { get; set; }
}

I ended up setting the Created and Updated properties of the BaseEntity using a columm attribute like this:

public abstract class BaseEntity
{
    [Key, Column(Order = 0)]
    public int Id { get; set; }
    [Column(TypeName = "datetime2")]
    public DateTime Created { get; set; }
    [Column(TypeName = "datetime2")]
    public DateTime Updated { get; set; } 
}

I looked at your solution at (http://dreadjr.blogspot.com/2012/09/entity-framework-5-code-first-datetime2.html) and it looks fine too, but it requires to have a specific configuration class for each entity. Anyways, I thought I'll share this with you.

like image 160
lopezbertoni Avatar answered Feb 04 '26 16:02

lopezbertoni