SubSonic 3 / ActiveRecord - 比较两个记录的简单方法?
使用 SubSonic 3 / ActiveRecord,是否有一种简单的方法可以比较两个记录,而无需逐列比较。 例如,我想要一个执行类似操作的函数(无需为数据库中的每个表编写自定义比较器):
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;
}
}
最后,我真正需要的是一个测试两行之间是否相等的函数,不包括主键列。 上面的伪代码是更通用的形式。 我相信,一旦获得不匹配的列,我将能够检查是否有任何列是主键字段。
我查看了 Columns 属性,但没有找到任何可以使用的东西。 理想情况下,解决方案是我可以将其放入 t4 文件并为数据库中的所有表生成的内容。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果使用 SQL Server 作为后端(因为可以自动填充),最好的方法是创建一个派生列,该列的定义使用 CHECKSUM 来散列“选定”列的值,以形成主键之外的唯一性。
编辑:如果您不使用 SQL Server,则需要在保存、编辑行时在代码中完成此哈希处理。
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.