Handling Dates with OData v4, EF6 and Web API v2.2

前端 未结 4 423
孤城傲影
孤城傲影 2020-12-10 16:14

I\'m in the midst of upgrading from v1-3 to v4, but I\'ve run into a few problems.

My understanding is that DateTime is unsupported, and I have to always use DateTim

4条回答
  •  隐瞒了意图╮
    2020-12-10 16:58

    You can add the AppendDatetimeOffset method to add automatically the methods using the microsoft T4 engine (i.e. updating the template file *.tt). So that when regenerating the code, you don't have to append classes again. Hope this Helps :)

    public string Property(EdmProperty edmProperty)
        {
            return string.Format(
                CultureInfo.InvariantCulture,
                (_ef.IsKey(edmProperty) ? "[Key]" : "") +
                "{0} {1} {2} {{ {3}get; {4}set; }}  {5}",           
                Accessibility.ForProperty(edmProperty),
                _typeMapper.GetTypeName(edmProperty.TypeUsage),
                _code.Escape(edmProperty),
                _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
                _code.SpaceAfter(Accessibility.ForSetter(edmProperty)),
                AppendDateTimeOffset(edmProperty));
        }
    
        public string AppendDateTimeOffset(EdmProperty edmProperty){
    
         if(!_typeMapper.GetTypeName(edmProperty.TypeUsage).Contains("DateTime")) return " ";
         //proceed only if date time
         String paramNull = @"public Nullable edm{0} 
                            {{
                                get
                                {{
                                    return {0}.HasValue ? new DateTimeOffset({0}.Value, TimeSpan.FromHours(0)) : (DateTimeOffset?)null;
                                }}
                            }}"; 
    
        String paramNotNull = @"public System.DateTimeOffset edm{0} 
                            {{
                                get
                                {{
                                    return new DateTimeOffset({0}, TimeSpan.FromHours(0));
                                }}
                            }}"; 
    
            String s= String.Empty;
            if(edmProperty.Nullable){
            s = string.Format(paramNull, edmProperty.Name);     
            }else
            {
            s = string.Format(paramNotNull, edmProperty.Name);      
            }
            return s;
    
        }
    

提交回复
热议问题