I am using ASP.NET MVC5 and MS SQL 2008 and EntityFramework 6, in my application i have a class Experiences that will allow the clients to add details of their experiences from period till period:
public class Experience
{
public int Id { get; set; }
public string Title { get; set; }
public string Company { get; set; }
public string FromDate { get; set; }
public string ToDate { get; set; }
public string Description { get; set; }
}
the FromDate format will be MMM yyyy, example Jan 2009, Oct 2010 , ...etc the ToDate format will be either MMM yyyy or Present and should be greater than or equal the FromDate value for example:
- From = Jan 2010, To= Oct 2014 ==> Accepted
- From = Jan 2010, To= Present ==> Accepted
- From = Oct 2014, To = Jul 2014 ==> Rejected the value to To Date should be greater than the From date
I made the class Period as follow
public class Period: IComparable
{
public Period()
{
this.Month="Present";
}
public Period(string Month,int Year)
{
this.Month=Month;
this.Year=Year;
}
public int CompareTo(object obj)
{
if(obj==null)
return 1;
Period period = obj as Period;
if (period != null)
{
if(this.Month=="Present" && period.Month=="Present")
return 0;
else if(period.Month=="Present")
return -1;
else if(this.Month=="Present")
return 1;
else
{
DateTime date;
DateTime periodDate;
if(!DateTime.TryParse(string.Format("01 {0} {1}",Month,Year),out date))
throw new ArgumentException("Instance is not a valid Period!");
if(!DateTime.TryParse(string.Format("01 {0} {1}",period.Month,period.Year),out periodDate))
throw new ArgumentException("Object is not a valid Period!");
return date.Date.CompareTo(periodDate.Date);
}
}
else
throw new ArgumentException("Object is not a Period");
}
public override int GetHashCode()
{
if(this==null)
return 0;
else
{
DateTime date;
if(!DateTime.TryParse(string.Format("01 {0} {1}",Month,Year),out date))
throw new ArgumentException("Instance is not a valid Period!");
return date.GetHashCode();
}
}
public override bool Equals(object obj)
{
return this.CompareTo(obj)==0;
}
public static bool operator==(Period left, Period right)
{
return left.Equals(right);
}
public static bool operator!=(Period left, Period right)
{
return !(left==right);
}
public static bool operator<(Period left, Period right)
{
return left.CompareTo(right)<0;
}
public static bool operator>(Period left, Period right)
{
return left.CompareTo(right)>0;
}
public override string ToString()
{
if(string.IsNullOrWhiteSpace(Month))
return string.Empty;
else if(Month=="Present")
return Month;
else
return string.Format("{0} {1}",Month,Year);
}
public string Month{get; set;}
public int Year{get; set;}
}
Sample DEMO
in sql i created the following datatype
create type Period from nvarchar(8) not null;
Questions:
- How can i define Period in sql?
- How can i tell EntityFramework to link period to the data type period in sql?
any idea will be appreciated