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 9020975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:08:53+00:00 2026-06-16T05:08:53+00:00

I need to use Sql Server’s datetime2 type on all the DateTime and DateTime?

  • 0

I need to use Sql Server’s “datetime2” type on all the DateTime and DateTime? properties of all my entity objects. This is normally done with the fluent API like this:

modelBuilder.Entity<Mail>().Property(c => c.SendTime).HasColumnType("datetime2");

However, I would prefer NOT to do this manually for each and every DateTime field in each and every entity type. (I do not have a common base type where all the DateTime properties can be placed, because the DateTime properties are specific to the entity types where they are defined).

The short question: What are my options ?

The long question: I was considering using reflection and made an atttempt, but it got very messy as is seems like the fluent API is not really suited for this kind of work because I had to call the generic fluent API methods via reflection. Here is my messy attempt:

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        var genericEntityMethod = modelBuilder.GetType().GetMethod("Entity");
        var entityTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.GetInterface("IEntity") != null);
        foreach (var t in entityTypes) {
            var props = t.GetProperties().Where(p => 
                p.GetSetMethod() != null && 
                p.GetGetMethod() != null && 
                (p.PropertyType == typeof (DateTime) ||  
                p.PropertyType == typeof(DateTime?)));
            foreach (var propertyInfo in props) {
                var entityMethod = genericEntityMethod.MakeGenericMethod(t.GetType());
                var entityTypeConfiguration = entityMethod.Invoke(modelBuilder,null);
                var lambdaExpression = Expression.Lambda(Expression.MakeMemberAccess(Expression.Parameter(t), propertyInfo), Expression.Parameter(t));

                //var propertyMethod = entityTypeConfiguration.GetType().GetMethod("Property");  // Cant get this to work
                var propertyMethods = entityTypeConfiguration.GetType().GetMethods().Where(m => m.ReturnType == typeof(DateTimePropertyConfiguration)).ToList();
                var propertyMethod = propertyInfo.PropertyType == typeof (DateTime) ? propertyMethods[0] : propertyMethods[1];

               var dateTimePropertyConfiguration = propertyMethod.Invoke(entityTypeConfiguration, new object[] {lambdaExpression});
                var hasColumnTypeMethod = entityTypeConfiguration.GetType().GetMethod("HasColumnType");
                hasColumnTypeMethod.Invoke(dateTimePropertyConfiguration, new object[]{ "datetime2" });
            }
        }
        base.OnModelCreating(modelBuilder);
    }

This fails in this line:

                   var dateTimePropertyConfiguration = propertyMethod.Invoke(entityTypeConfiguration, new object[] {lambdaExpression});

with this error (Entities.Order is one of my entity objects having a DateTime property):

Object of type 'System.Linq.Expressions.Expression`1[System.Func`2[Entities.Order,System.Nullable`1[System.DateTime]]]' cannot be converted to type 'System.Linq.Expressions.Expression`1[System.Func`2[System.RuntimeType,System.Nullable`1[System.DateTime]]]

Can anyone help me out with this reflection approach or better, show me a simpler way to avoid having to manually write a lot of fluent api stuff?

  • 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-06-16T05:08:54+00:00Added an answer on June 16, 2026 at 5:08 am

    There is much better approach. Unfortunately the approach is based on custom conventions which will be available in EF6. You can try EF6 Alpha 2 and play with custom conventions yourselves – the walkthrough contains example with your exact requirement.

    To solve your issue with reflection check this blog article. It describes the code for generating mappings with reflection (there should be related open source project).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use the clock on my SQL Server to write a time
I am creating a window application that need to use sql server database. I
In a web application using Entity Framework 4.1 we use SQL Server Membership to
I use SQL Server 2008. I need to DENY any delete operations on a
I need to export data from a SQL database (some groups use SQL Server,
I am working with SQL Server 2005. I need to select all rows from
I use SQL Server 2008 R2, I need to rebuild the index for every
I need use case condition in sql server, the datatype is money, the condition
I use SQL Server Management Studio to manage my SQL Server. I need to
I rarely use SQL and have a unique issue I need to solve. I

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.