一
ComlexType
可以处理这个。我创造了一种类型
LeagcyDate
它将int值存储在
DbData
属性,并添加了到日期和从日期的隐式转换。这种方法似乎是可以合理管理的。由于模型设计器中支持EF4 complextypes,因此添加一个类型将不再调用篡改EDMX。
我在这样的分部类中所做的隐式转换:
public partial class LegacyDate
{
public static implicit operator DateTime?(LegacyDate value)
{
return LegacyDateConverter.ToDate(value.DbData);
}
public static implicit operator LegacyDate(DateTime? value)
{
return new LegacyDate { DbData = LegacyDateConverter.ToLegacyDate(value) };
}
}
internal static class LegacyDateConverter
{
public static DateTime? ToDate(int data)
{
if (data == 0) return null;
int year = data/10000;
int month = (data%10000)/100;
int day = data/1000000;
DateTime date;
try
{
date = new DateTime(year, month, day);
}
catch (Exception e)
{
throw new ArgumentException(String.Format(
"Value {0} kan not be converted to DateTime", data), e);
}
return date;
}
public static int ToLegacyDate(DateTime? value)
{
int dateInt = 0;
if (value.HasValue)
{
dateInt =
value.Value.Year*10000 + value.Value.Month*100 + value.Value.Day;
}
return dateInt;
}
}