here's a workaround on how to do this
your experience class will be like this:
public class Experience { public int Id { get; set; } public string Title { get; set; } public string Company { get; set; } public Period FromDate { get; set; } public Period ToDate { get; set; } public string Description { get; set; } }
change the following in your period class to be [ComplexType]
// change the folllowing [NotMapped] public string Month{get; set;} [NotMapped] public int Year{get; set;} // add this property to use it for EntityFramework mapping public string Date { get { return ToString(); } set { if (!string.IsNullOrEmpty(Date)) { if (Date == "Present") Month = "Present"; else { var split = Date.Split(' '); Month = split[0]; Year = Convert.ToInt32(split[1]); } } } }
in your mapping you do the following
modelBuilder.Entity<Experience>() .Property(t => t.FromDate.Date) .IsRequired() .HasMaxLength(8); modelBuilder.Entity<Experience>() .Property(t => t.ToDate.Date) .IsRequired() .HasMaxLength(8);
so that the entity infrastructure creates fields as a period data type, you can do this 1- enable-migration 2- add-migration first // this will create a dbmigration file that has a database structure and tables 3- in the Up function at the end you write the following, using Sql (@ "") ;, here I will put sql instructions for you
create function CheckPeriod(@period nvarchar(8)) -- this function will make sure that Period is valid returns bit as begin declare @month nvarchar(3) if ( @period is null ) return 0 if( @period = 'Present') return 1; set @month=substring(@period,0,3) if(@month in ('Jan','Feb','Mar','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')) return 1; return isdate(@period); end create function CheckValidity(@right as nvarchar(8),@left as nvarchar(8)) -- this function will compare FromDate to ToDate and return valid if ToDate>FromDate returns bit as begin if(@right='Present') return 0; if(@left='Present' and cast(@right as date)>=getdate()) return 0; if(@left='Present') return 1; if(cast(@left as date)>cast(@right as date)) return 1; return 0; end exec sp_addtype 'Period','nvarchar(8)','NOT NULL' alter table Experiences alter column FromDate Period alter table Experiences alter column ToDate Period alter table Experiences add constraint FromDateIsPeriod check ( dbo.CheckPeriod(FromDate)=1) alter table Experiences add constraint ToDateIsPeriod check ( dbo.CheckPeriod(ToDate)=1) alter table Experiences add constraint PeriodValidity check ( dbo.CheckValidity(FromDate,ToDate)=1)
and in the Down function, if you want you to write code to drop functions and change table fields to return them as nvarchar (8) and finally delete the Period data type
hope this helps you