SubSonic 3 / ActiveRecord – Easy way to compare two records

subsonicsubsonic3

With SubSonic 3 / ActiveRecord, is there an easy way to compare two records without having to compare each column by column. For example, I'd like a function that does something like this (without having to write a custom comparer for each table in my database):

public partial class MyTable
{
    public IList<SubSonic.Schema.IColumn> Compare(MyTable m)
    {
        IList<SubSonic.Schema.IColumn> columnsThatDontMatch = new...;
        if (this.Field1 != m.Field1)
        {
            columnsThatDontMatch.add(Field1_Column);
        }
        if (this.Field2 != m.Field2)
        {
            columnsThatDontMatch.add(Field2_Column);
        }
        ...
        return columnsThatDontMatch;
    }
}

In the end, what I really need is a function that tests for equality between two rows, excluding the primary key columns. The pseudo-code above is a more general form of this. I believe that once I get the columns that don't match, I'll be able to check if any of the columns are primary key fields.

I've looked through Columns property without finding anything that I can use. Ideally, the solution would be something I can toss in the t4 file and generate for all my tables in the database.

Best Answer

The best way, if using SQL Server as your backend as this can be auto populated, is to create a derived column that has a definition that uses CHECKSUM to hash the values of "selected" columns to form a uniqueness outside of the primary key.

EDIT: if you are not using SQL Server then this hashing will need to be done in code as you save, edit the row.