我需要向表中插入一条记录。Subsonic:如何排除一个表列,以便它不包含在生成的SQL查询中
亚音速建立查询是这样的(据我所知):
INSERT INTO Table1
(Title, Description, RowVersion)
VALUES
(@Title, @Description, @RowVersion)
但我想从SQL查询中删除RowVersion列bacause其通过在SQL Server自动生成。 我该怎么做?
我需要向表中插入一条记录。Subsonic:如何排除一个表列,以便它不包含在生成的SQL查询中
亚音速建立查询是这样的(据我所知):
INSERT INTO Table1
(Title, Description, RowVersion)
VALUES
(@Title, @Description, @RowVersion)
但我想从SQL查询中删除RowVersion列bacause其通过在SQL Server自动生成。 我该怎么做?
您不必担心这一点。 SubSonic足够智能来处理这个问题!
只需为属性创建新的对象分配值并保存即可。
var o = new DataObject();
o.Name="Foo";
o.Age = 20;
//o.RowVersion = ....; DON'T ASSIGN THIS
o.Save();
编辑: - 以下是我已经试过:
表定义:
CREATE TABLE [dbo].[TestTimeStamp](
[RowID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Description] [nvarchar](50) NOT NULL,
[RowVersion] [timestamp] NOT NULL
)
代码:
private static void Test()
{
var o = new TestTimeStamp();
o.Description = "Hello World";
o.Save();
}
固定: - Yippe,我纺丝我的头在原因,因为这从来没有发生在SubSonic 2中。我分支SubSonic 3代码,但没有什么可以找到的。然后,经过多次愚弄,我再次检查了T4模板。一些IsReadOnly
属性没有被设置,但在插入,更新SubSonic.Extension.Object.cs类中的查询时进行检查。所以解决方法是在Structs.tt
文件的for
循环中添加一行,它将列添加到表类:)。要修复找到下面的循环(它开始于第30行)新DatabaseColumn的
<# foreach(var col in tbl.Columns){#>
Columns.Add(new DatabaseColumn("<#=col.Name#>", this)
{
和变革初始化如下:
Columns.Add(new DatabaseColumn("<#=col.Name#>", this)
{
IsPrimaryKey = <#=col.IsPK.ToString().ToLower()#>,
DataType = DbType.<#=col.DbType.ToString()#>,
IsNullable = <#=col.IsNullable.ToString().ToLower()#>,
AutoIncrement = <#=col.AutoIncrement.ToString().ToLower()#>,
IsForeignKey = <#=col.IsForeignKey.ToString().ToLower()#>,
//THIS LINE DO THE TRICK.
IsReadOnly = <#=col.DataType.ToLower().Equals("timestamp")
.ToString().ToLower() #>
});
PS: - 请here获得亚音速srouce。在以前的版本中,只有null和AutoIncrement
被检查包含到添加和更新列表中,但此代码也检查ReadOnly属性。
这是不工作.. RowVersion是必填字段.. RowVersion是一个时间戳列.. – h3n 2009-10-12 05:40:11
您正在使用哪个数据库? – TheVillageIdiot 2009-10-12 06:00:59
是行版本的时间戳类型? – TheVillageIdiot 2009-10-12 06:09:12
我对时间戳字段感到非常难过,所以只是完全排除了Structs.tt中的timestamp列。这是使用目前SubSonic 3.0主结账
if(<#=col.DataType.ToLower().Equals("timestamp").ToString().ToLower() #>)
{}else{
Columns.Add(new DatabaseColumn("<#=col.Name#>", this)
{
IsPrimaryKey = <#=col.IsPK.ToString().ToLower()#>,
DataType = DbType.<#=col.DbType.ToString()#>,
IsNullable = <#=col.IsNullable.ToString().ToLower()#>,
AutoIncrement = <#=col.AutoIncrement.ToString().ToLower()#>,
IsForeignKey = <#=col.IsForeignKey.ToString().ToLower()#>,
MaxLength = <#=col.MaxLength#>
});
}
我已经更新了答案,请看看它! – TheVillageIdiot 2009-10-12 15:19:15