Note that there are some explanatory texts on larger screens.

plurals
  1. POChange data type of properties inherited from the base class in Entity Framework 5 code first
    primarykey
    data
    text
    <p>I am working on an application that uses EF 5 Code First against a SQL Server database. Some of my domain objects inherit from the base abstract class <code>DomainEntity</code> that contains shared properties, such as <code>Id</code>, <code>DateCreated</code>, <code>DateModified</code>, etc.</p> <p>By default EF creates <code>datetime</code> columns for properties of type <code>DateTime</code>. I have been looking for a way to tell it to create <code>smalldatetime</code> columns instead for all subclasses of <code>DomainEntity</code>.</p> <p>I've tried the following:</p> <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Ignore&lt;DomainEntity&gt;(); modelBuilder.Entity&lt;DomainEntity&gt;() .Property(e =&gt; e.DateCreated).HasColumnType("smalldatetime"); } </code></pre> <p>Unfortunately the above has no effect. And if I remove the <code>Ignore&lt;DomainEntity&gt;</code> line then EF creates a separate table called <code>DomainEntities</code>.</p> <p>The only approach that seems to work is to explicitly map <code>DateTime</code> properties in each subclass of <code>DomainEntity</code>, but obviously it is impractical because I have a couple of dozen of such classes.</p> <p>Is what I'm trying to do possible at all?</p> <hr> <h2>UPDATE</h2> <p>It works when I decorate the base class' date properties with the data annotation <code>[System.ComponentModel.DataAnnotations.Schema.Column(TypeName="smalldatetime")]</code>.</p> <p>Although I would really prefer to keep this DB-specific mapping information in the EF context and out of my POCO's.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload