使用PetaPoco设置默认的datetime值 [英] Set default datetime value with PetaPoco

查看:148
本文介绍了使用PetaPoco设置默认的datetime值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PetaPoco类,它定义了一个数据库表。它看起来像这样:

 命名空间MyProject.Pocos 
{
[TableName(AdminNotification)]
[PrimaryKey(id,autoIncrement = true)]
[ExplicitColumns]
public class AdminNotification
{
[Column(id)]
[PrimaryKeyColumn(AutoIncrement = true)]
public int id {get;组; }

[Column(dateTime)]
public DateTime dateTime {get;组; }

[Column(adminNotificationTypeId)]
public int adminNotificationTypeId {get;组; }
}
}

除了一件事情之外,它的功能非常好。在数据库表本身(在SQL Server Express中)有一个为'dateTime'设置的默认值 - 它默认为(getdate())。但是,当我的代码中使用PetaPoco类插入记录时,dateTime的值始终为空。



如何将PetaPoco类中的默认值设置为当前日期/时间?



谢谢!

解决方案

添加一个构造函数并在其中设置默认值:

  [TableName(AdminNotification)] 
[PrimaryKey id,autoIncrement = true)]
[ExplicitColumns]
public class AdminNotification
{
[Column(id)]
[PrimaryKeyColumn(AutoIncrement = true )]
public int id {get;组; }

[Column(dateTime)]
public DateTime dateTime {get;组; }

[Column(adminNotificationTypeId)]
public int adminNotificationTypeId {get;组; }

public AdminNotification(){
dateTime = DateTime.Now;
}

}

根据您创建的方式和插入对象,该值将显示创建AdminNotification的时间,而不是实际写入数据库的时间,但大部分时间的差异可以忽略不计,并不会有所作为。


I have a PetaPoco class which defines a database table. It looks like this:

namespace MyProject.Pocos
{
    [TableName("AdminNotification")]
    [PrimaryKey("id", autoIncrement = true)]
    [ExplicitColumns]
    public class AdminNotification
    {
        [Column("id")]
        [PrimaryKeyColumn(AutoIncrement = true)]
        public int id { get; set; }

        [Column("dateTime")]
        public DateTime dateTime { get; set; }

        [Column("adminNotificationTypeId")]
        public int adminNotificationTypeId { get; set; }
    }
}

It works great except for one thing. In the database table itself (in SQL Server Express) there is a default value set for 'dateTime' - it defaults to (getdate()). However, when record is inserted using the PetaPoco class in my code, the value of dateTime is always NULL.

How can I set the default value in the PetaPoco class to the current date/time?

Thanks!

解决方案

One way is to add a constructor and set the default value there:

    [TableName("AdminNotification")]
    [PrimaryKey("id", autoIncrement = true)]
    [ExplicitColumns]
    public class AdminNotification
    {
        [Column("id")]
        [PrimaryKeyColumn(AutoIncrement = true)]
        public int id { get; set; }

        [Column("dateTime")]
        public DateTime dateTime { get; set; }

        [Column("adminNotificationTypeId")]
        public int adminNotificationTypeId { get; set; }

        public AdminNotification(){
          dateTime = DateTime.Now;
        }

    }

Depending on the way you create and insert the object, the value will be showing the time of creation of the AdminNotification, not the time it has actually been written to the database, but most of the time the difference is negligible and won't make a difference.

这篇关于使用PetaPoco设置默认的datetime值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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