Mapping TimeSpan to SQL Time column issue

2 posts, 0 answers
  1. Dainius
    Dainius avatar
    2 posts
    Member since:
    Oct 2011

    Posted 05 Sep Link to this post

    I am trying to map a SQL time column to a TimeSpan field, everything is fine if I try to retrieve the data from the SQL Database and display it, however when I try to Insert or Update an object, the Open Access library throws "Operant type clash: int is incompatible with time...", is there anything I am missing in the fluent model specification, the only thing I am specifying in the fluent model metadata class is the primary key

    private static MappingConfiguration<T> MapObject<T>(Expression<Func<T, object>> mapFunc, string tableName)
            {
                var mapping = new MappingConfiguration<T>();
                mapping.MapType(mapFunc).ToTable(tableName);
                return mapping;
            }
    private static void MapDefinedObject(List<MappingConfiguration> configurations)
            {
                var mapping = MapObject<DefinedObject>(e => new
                {
                    Id = e.Id, //int
                    Time = e.Time, //Nullable<TimeSpan>
                    Day = e.Day, //Nullable<DateTime>
                }, "DefinedObject");
                previousFireMapping.FieldNamingRules.AddPrefix = "_";
                previousFireMapping.FieldNamingRules.CaseMode = CaseChangeModes.Lower;
                previousFireMapping.HasProperty(e => e.Id).IsIdentity();
                configurations.Add(mapping );
            }

     

  2. Dainius
    Dainius avatar
    2 posts
    Member since:
    Oct 2011

    Posted 05 Sep in reply to Dainius Link to this post

    I managed to solve the problem (as cleanly as possible) by changing the type of private field to DateTime? keeping the public field TimeSpan? and mapping the private fields to the Database, example code if anyone experiences the same issue:

    public partial class DefinedObject
    {
        public virtual int Id { get; set; }
     
        public virtual DateTime? Day { get; set;}
     
        private DateTime? _time;
     
        public virtual TimeSpan? Time
        {
            get { return !_time.HasValue ? null : _time.Value.TimeOfDay as TimeSpan?; }
            set { _time = !value.HasValue ? null : new DateTime?(DateTime.Today.AddSeconds(value.Value.TotalSeconds)); }
        }
    }
     
    //Metaddata class mapping
    private static MappingConfiguration<T> MapObject<T>(Expression<Func<T, object>> mapFunc, string tableName)
            {
                var mapping = new MappingConfiguration<T>();
                mapping.MapType(mapFunc).ToTable(tableName);
                return mapping;
            }
            private static void MapDefinedObject(List<MappingConfiguration> configurations)
            {
                var mapping  = MapObject<DefinedObject>(e => new
                {
                    Id = e.Id,
                    Day = e.Day
                }, "DefinedObject");
                previousFireMapping.FieldNamingRules.AddPrefix = "_";
                previousFireMapping.FieldNamingRules.CaseMode = CaseChangeModes.Lower;
                previousFireMapping.HasProperty(e => e.Id).IsIdentity();
                previousFireMapping.HasPrimitiveField("_time").HasColumnType("time").ToColumn("Time");
     
                configurations.Add(previousFireMapping);
            }

     

    I hope this helps someone.

  3. DevCraft banner
Back to Top