问题
Using Subsonic 3, Simple Repository the Add always gives me a 00000000-0000-0000-0000-000000000000 for my PK which obviously causes an error in my DB. I see that the definition of my PK created by Subsonic is using newid() but still having this issue.
Anyone else having this problem?
FYI I am using a local SQL Server 2005 DB sitting in my App_Data directory.
回答1:
When you use Guid's SubSonic expects you to set them. Your best bet is to Guid.NewGuid() in the constructor. Remember that the SimpleRepo is "POCO-first" thinking. The DB is just a place for data.
回答2:
I'm also using a GUID as a PK. My PK default setting is...
(CONVERT([uniqueidentifier],CONVERT([binary](6),getdate(),(0))+CONVERT([binary](10),newid(),(0)),(0)))
I've been going through the subsonic source and I'm beginning to think that this is related to the fact that... shiz I don't quite know.
I do know that Subsonic isn't seeing the NOT NULL, PK, Default Setting having column as READ ONLY.
So, assuming you don't have any other PK datatypes in the solution, you could add a few lines to the Structs.tt file...
Find the foreach loop on line 30...
<# foreach(var col in tbl.Columns){#>
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()#>
<# if (col.IsPK.ToString().ToLower() == "true"){ #>
, IsReadOnly = <#=col.IsPK.ToString().ToLower()#>
<# } #>
});
The section you're adding is the IF statement just after IsForeignKey.
This should make it work for your your PK Guids so long as you have something in default setting.
I can't make any guarantees about any other PK's you may have in the DB though. You've been warn'd.
来源:https://stackoverflow.com/questions/1184622/subsonic-3-guid-as-pk-always-gives-me-00000000-0000-0000-0000-000000000000