开发者

How can I allow sql to set datetime with subsonic

开发者 https://www.devze.com 2023-01-25 06:42 出处:网络
I have begun using SubSonic and I am wondering how to get it to ignore DateTime properties on insert. I am using the ActiveRecord template and have default methods to create the datetime properties on

I have begun using SubSonic and I am wondering how to get it to ignore DateTime properties on insert. I am using the ActiveRecord template and have default methods to create the datetime properties on insert on the DB side.

When I try to insert I get the following: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

I would rather not have to set the datetime in code as all items will drop 开发者_StackOverflowinto a queue and I'd like their created on datetime to be the actual time the record was created. Is there any way to get this behavior?


Override the BeforeInsert() and BeforeUpdate() methods (Tested and confirmed) with SubSonic 2 and set the Columns you want to ReadOnly.

public partial class Products
{
    protected override void BeforeInsert()
    {
        Schema.GetColumn(Columns.CreatedOn).IsReadOnly = true;
        base.BeforeInsert();
        Schema.GetColumn(Columns.CreatedOn).IsReadOnly = false;
    }

    protected override void BeforeUpdate()
    {
        Schema.GetColumn(Columns.ModifiedOn).IsReadOnly = true;
        base.BeforeUpdate();
        Schema.GetColumn(Columns.ModifiedOn).IsReadOnly = false;
    }
}

Schema is static so you can do this once in your code (even from outside the class) and don't worry about it again but I would prefer this way.

I haven't tried this with SubSonic3 but the IsReadOnly Property is still there so it should work, too.


If you're talking about the SubSonic ORM for .NET - you might try asking this question on the SubSonic support blog or send and email to subsonicproject@googlegroups.com requesting help.


Try to define DateTime as parameter. The similar situation is described here - http://www.devart.com/blogs/dotconnect/?p=2982#first.

0

精彩评论

暂无评论...
验证码 换一张
取 消