This is a migrated thread and some comments may be shown as answers.

Mapping TimeSpan to SQL Time column issue

1 Answer 153 Views
Databases and Data Types
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dainius
Top achievements
Rank 1
Dainius asked on 05 Sep 2016, 11:41 AM

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 );
        }

 

1 Answer, 1 is accepted

Sort by
0
Dainius
Top achievements
Rank 1
answered on 05 Sep 2016, 01:18 PM

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.

Tags
Databases and Data Types
Asked by
Dainius
Top achievements
Rank 1
Answers by
Dainius
Top achievements
Rank 1
Share this question
or