在数据库中只存储日期时间部分C# [英] store only date in database not time portion C#

查看:693
本文介绍了在数据库中只存储日期时间部分C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试类和一个 ExecutionDate 属性,只存储日期,但是当我们使用 [DataType(DataType.Date)] 也将时间部分存储在数据库中,但我只想要日期部分。

I have a test class and an ExecutionDate property which stores only date but when we use [DataType(DataType.Date)] that also stores the time portion in database but I want only date portion.

public class Test
{
     [Key]
     public int Id { get; set; }

     [DataType(DataType.Date)]
     public DateTime ExecutionDate { get; set; }      
}

使用实体框架在数据库中只存储日期时间部分?请帮助我....

Is any way to store only date on time portion in db using Entity Framework? Please help me....

当使用 [DataType(DataType.Date)] 时,我添加了快照存储时间部分00:00我想删除

I have added snapshot when use [DataType(DataType.Date)] that stores time portion 00:00 I want remove that

推荐答案

我想你试图指定数据库列类型。您可以使用文章中所述的数据注释。

I think you are trying to specify database column type. You could use data annotations as described in this article.

这是一个例子:

[Table("People")]
public class Person
{
    public int Id { get; set; }

    [Column(TypeName = "varchar")]
    public string Name { get; set; }

    [Column(TypeName="date")]
    public DateTime DOB { get; set; }
}

默认情况下,字符串被翻译成nvarchar,我们在这里改变了。另外Datetime(这是你想问的),它默认映射到sql server中的datatime,被更改为 date ,它只存储日期部分而不是时间部分一个 DateTime 值。

By default, string is translated to nvarchar, we have changed that here. Also Datetime (this is what you asked I suppose) which by default maps to datatime in sql server, is changed to date which stores only the date portion and not the time portion of a DateTime value.

这篇关于在数据库中只存储日期时间部分C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆