EF:如何使用由 Datetime 类型的数据库列支持的 Edm.Time 类型的字段?
我有一个 SQL 表,其中有一列日期时间类型和一个要匹配的实体模型。 我真正想要的是我的实体属性是时间类型,因为我只关心时间部分。但是,如果我在模型中将类型从 DateTime 切换为 Time,EF 会抱怨,并且我无法更新数据库以匹配。
我应该怎么办?
编辑-这是部分类:
public partial class LineSchedule
{
public TimeSpan TimeOfDay
{
get
{
TimeSpan ts = new TimeSpan(XXTimeOfDay.Hour, XXTimeOfDay.Minute, XXTimeOfDay.Second);
return ts;
}
set
{
this.XXTimeOfDay = new DateTime(XXTimeOfDay.Year, XXTimeOfDay.Month, XXTimeOfDay.Day,
value.Hours, value.Minutes, value.Seconds);
}
}
}
I've got a SQL table that has a column of type datetime, and an entity model to match.
What I'd really like is for my Entity property to be of type Time, since I only care about the time portion. However, if I switch the type from DateTime to Time in the model, the EF complains, and I can't update the database to match.
What should I do?
Edit- Here's the partial class:
public partial class LineSchedule
{
public TimeSpan TimeOfDay
{
get
{
TimeSpan ts = new TimeSpan(XXTimeOfDay.Hour, XXTimeOfDay.Minute, XXTimeOfDay.Second);
return ts;
}
set
{
this.XXTimeOfDay = new DateTime(XXTimeOfDay.Year, XXTimeOfDay.Month, XXTimeOfDay.Day,
value.Hours, value.Minutes, value.Seconds);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于实体框架生成分部类,我会:
更改生成的属性的名称
将 getter 和 setter 设为私有
在单独的部分声明中创建公共属性
使用它的 getter 和 setter 将生成的日期时间包装为时间。
Since Entity Framework produces partial classes, I would:
Change the name of the generated property
Make both the getter and setter private
Create a public property in a separate partial declaration
Use it's getter and setter to wrap the generated datetime as a Time.