Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 882877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:29:41+00:00 2026-05-15T12:29:41+00:00

I inherited an old Database where dates are stored as Int32 values (times too,

  • 0

I “inherited” an old Database where dates are stored as Int32 values (times too, but dates shall suffice for this example) i.e. 20090101 for Jan. 1st 2009.
I can not change the schema of this database because it is still accessed by the old programs.
However I want to write a new program using EF as the O/R-M.
Now I’d love to have DateTime-Values in my Model.

Is there a way to write a custom type and use this as a Type in the EF-Model.

Something like a Type “OldDate”
with properties:
DatabaseValue : Int23
Value : DateTime (specifying the date-part whereas the time-part is always 0:00:00)

Is something like this possible with the EF-Model ?

Edit:
I think I found what I was looking for – but in NHibernate, so I’ll rephrase:
Is something like NHIbernates IUserType possible with EF?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:29:41+00:00Added an answer on May 15, 2026 at 12:29 pm

    A ComlexType can manage this. I created a Type LeagcyDate which stores the int value in a DbData property and added an implicit conversion to and from Date. This approach seems reasonably manageble. Since EF4 ComplexTypes are supported in the Model-Designer, so adding one does no longer invoke tampering with the edmx.

    The implicit Conversion I have done in a partial class like so:

        public partial class LegacyDate
        {
          public static implicit operator DateTime?(LegacyDate value)
          {
              return LegacyDateConverter.ToDate(value.DbData);
          }
    
          public static implicit operator LegacyDate(DateTime? value)
          {
              return new LegacyDate { DbData = LegacyDateConverter.ToLegacyDate(value) };
          }
       }
    
        internal static class LegacyDateConverter
        {
            public static DateTime? ToDate(int data)
            {
                if (data == 0) return null;
                int year = data/10000;
                int month = (data%10000)/100;
                int day = data/1000000;
                DateTime date;
    
                try
                {
                    date = new DateTime(year, month, day);
                }
                catch (Exception e)
                {
                    throw new ArgumentException(String.Format(
                        "Value {0} kan not be converted to DateTime", data), e);
                }
                return date;
            }
    
            public static int ToLegacyDate(DateTime? value)
            {
                int dateInt = 0;
                if (value.HasValue)
                {
                    dateInt =
                    value.Value.Year*10000 + value.Value.Month*100 + value.Value.Day;
                }
                return dateInt;            
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited a very old database that needs some data to be updated.
I am working on an old database I inherited from my predecessors. In it,
I've just inherited an old WinForms app with a UI layout like this: I'm
I inherited a database from an old system we have, in which (among other
This is my problem: I have an old database, with no constraints whatsoever. There
I have a stored procedure in an old SQL 2000 database that takes a
I have inherited an old application that stores a zip file in a database
I have inherited a old access database that includes a table with data organised
I have inherited an old crusty PHP application , and I'd like to refactor
A friend of mine has inherited an old Joomla 1.0.x site, that amongst other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.