Question

I have started using Subsonic for developing a small CMS web application. I noticed that when ever I call the .Save() method of the Subsonic-Table-Object, that my date field called 'CreatedOn' gets overwritten with today's date.

Subsonic Info: Version 3.0.0.4 using ActiveRecord

    // add article from dataset
    Article a = new Article(); 
    a.Title = article["title"].ToString();
    a.Synopsis = article["teaser"].ToString();
    a.Body = article["body"].ToString();
    a.Keywords = string.Empty;
    a.Photo = string.Empty;
    ///articles/2010/04/14/deregistration-of-trade-unions
    a.CreatedOn = DateTime.Parse(sDestin.Substring(9, 10)); 
    a.UpdatedOn = DateTime.Parse(sDestin.Substring(9, 10));
    a.Save();

Before saving, the CreatedOn field is set to the correct date (same as UpdatedOn). As soon as the .Save() method is run, the CreatedOn field changes to today's date. The UpdatedOn date field maintains the same date that was assigned to it.

What I have tried: I've double checked the Article table, there are no default values such as (GetDate()) present for any of the date fields. I've rebuilt my templates several times. I've also attempted to reset and save the CreatedOn date field a second time.

Any help would be appreciated.


Answer

After further investigation I found the problem. Subsonic appears to be generating a line of code which is manipulating the date. I dont know why its doing this only for the CreatedOn date on not the UpdatedOn date. Both fields are identical in structure (MSSQL 2005).

Extracted from the Save() method of my article object.

        public void Add(IDataProvider provider){

        //this.CreatedOn=CMSDB.DateTimeNowTruncatedDownToSecond(); // commenting this out works, CreatedOn is no longer overwritten

        var key=KeyValue();
        if(key==null){
            var newKey=_repo.Add(this,provider);
            this.SetKeyValue(newKey);
        }else{
            _repo.Add(this,provider);
        }
        SetIsNew(false);
        OnSaved();
    }

Here is the anomolous context method Subsonic created:

        internal static DateTime DateTimeNowTruncatedDownToSecond() {
        var now = DateTime.Now;
        return now.AddTicks(-now.Ticks % TimeSpan.TicksPerSecond);
    }

Note: Stackoverflow required me to wait 6 hours to post the answer so I placed it here instead.

Was it helpful?

Solution

"CreatedOn" is a convention in SS ActiveRecord - when a field is called "CreatedOn" the code that you see to set the date is added by the T4 templates. It clearly isn't helpful in your case.

Two options:

  • edit your T4 template to remove the offending line (so you will not be able to take advantage of the auto-time-stamping in any of your model objects)

  • change the name of the field to something else

OTHER TIPS

If accepted answer is not applicable for you as with my case, you might do a

new InlineQuery().Execute(string query)

to update the said fields.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top