locked
DbCommandInterceptionContext ReaderExecuting in EF 6 RRS feed

  • Question

  • Hi Team,

    http://xabikos.com/2014/11/18/Create-a-multitenant-application-with-Entity-Framework-Code-First-Part-2/

    Am using the above link, for DbCommandInterceptionContext ReaderExecuting in EF 6.

    While reading the DBset, i need to find the Datetime column in the EntitySet,

    And convert the value from Datetime.Utc to Datetitme.ToLocal.

    i tried the with below sample code

     public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
            {
                OnStart(command);
            }

     private static void OnStart(DbCommand command)
            {
                foreach (DbParameter para in command.Parameters)
                {                
                    if (para.DbType.ToString() == "DateTime")
                    {
                        DateTime localValue = ((DateTime)para.Value).ToLocalTime();
                        m_StartTime.TryAdd(command, localValue);
                    }
                }        
            }

    But am unable to get the value from the "localValue"

    Suggestion or solution Please

    Thanks in Advance

    Monday, July 4, 2016 4:58 AM

Answers

All replies

  • Hi baskarg83,

    >>But am unable to get the value from the "localValue"

    According to your description and code snippet, I create a demo by using your code snippet, it seems that we couldn't retrieve database data type from command.Parameters.

    We could use convert it in our model class by using get/set method, like this:

    namespace DbCommandInterceptionDemo
    {
        using System;
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
    
        [Table("Class")]
        public partial class Class
        {
            private DateTime? _ModifyDate;
    
            public DateTime? ModifyDate
            {
                get { return Convert.ToDateTime(_ModifyDate).ToLocalTime(); }
                set { _ModifyDate = value; }
            }
            public int Id { get; set; }
    
            [Required]
            [StringLength(50)]
            public string ClassID { get; set; }
    
            [StringLength(50)]
            public string Student { get; set; }
    
            [Column(TypeName = "date")]
            public DateTime? BirthDate { get; set; }
    
            public DateTime? CreateDate { get; set; }
    
            //public DateTime? ModifyDate { get; set; }
    
            public DateTimeOffset? TestDate { get; set; }
        }
    }
    

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 5, 2016 3:18 AM
  • Hi cole,

    Thanks for your reply

    i dont want to do this in Model, i want to implement this in the EnityFramework Interceptor

    i have look into this link, same kind of conversion has to be implement for datetime

    How can I configure Entity Framework to automatically trim values retrieved for specific columns mapped to char(N) fields?

    Code:

     public class ConvertToLocalDateTime : DefaultExpressionVisitor
     {
      private static readonly string[] _typesToTrim = { "varchar", "datetime", String" };
      public override DbExpression Visit(DbNewInstanceExpression expression)
        {
            var arguments = expression.Arguments.Select(a =>
            {
                var propertyArg = a as DbPropertyExpression;
                if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name))
                {
    
                    DateTime dt = Convert.ToDateTime(a);                    
                    DbExpression outPutExp = dt.ToLocalTime();
                    return outPutExp;
                   // return EdmFunctions.Trim(a);
    
                }
    
                return a;
            });
    
            return DbExpressionBuilder.New(expression.ResultType, arguments);
        }
    }

    In the above code, am unable to convert "a" to Datatime.

    Thanks

    Tuesday, July 5, 2016 6:15 AM
  • Hi baskarg83,

    According to your description, it seems that you need to use EdmFunctions to convert it, from the following document about EdmFunctions, we could use year, month and day method to combine a datetime value.

    https://msdn.microsoft.com/en-us/library/system.data.common.commandtrees.expressionbuilder.edmfunctions(v=vs.110).aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 13, 2016 7:29 AM