Search code examples
c#subsonicsubsonic-active-record

SubSonic won't save an updated record with ActiveRecord?


I have a bit of a unique database structure but the query I'm attempting to do isn't too out of the ordinary, I think.

Basically, I create a new record. Then, I save this record, then I update a bit of XML and then update the record with the new XML. I'm having problems in that the second save doesn't do anything.

    var applicant = new Applicant();
    applicant.XmlData = "";
applicant.Save(); //save once and initiate the record
    data.RecordRID = applicant.ApplicantRID;
    applicant.XmlData = data.SerializeToXml();
    var c=applicant.GetDirtyColumns().Count; //this returns a count of 0
    applicant.Save(); //save twice to populate RecordRID

Also, for reference, the XmlData property generated looks like this:

    public string XmlData
    {
        get { return _XmlData; }
        set
        {
            if(_XmlData!=value){
                _XmlData=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="XmlData");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ //_isLoaded is never set to true for either saves
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

So.. What is the problem? Do I need to run a query and get the object from the database and then update it? or am I missing something?

Note: I have to save this twice because the ApplicantRID is a unique primary key. So I have to insert a record before I can know what it is


Solution

  • I ended up modifying the T4 templates of SubSonic to fix this.

    In ActiveRecord.tt around line 466 under the function void Add(IDataProvider):

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

    I have not noticed any weirdness from this change, and it seems like a bug that it wasn't already in there as it is.