简体   繁体   English

使用SqlServer uniqueidentifier /更新日期列与Linq到Sql - 最佳方法

[英]Using SqlServer uniqueidentifier/updated date columns with Linq to Sql - Best Approach

Rightly or wrongly, I am using unique identifier as a Primary Key for tables in my sqlserver database. 无论是对还是错,我使用唯一标识符作为我的sqlserver数据库中表的主键。 I have generated a model using linq to sql (c#), however where in the case of an identity column linq to sql generates a unique key on inserting a new record for guid / uniqueidentifier the default value of 00000000-0000-0000-0000-000000000000 . 我已经使用linq to sql(c#)生成了一个模型,但是在identity列的情况下,linq to sql在为guid / uniqueidentifier插入新记录时生成一个唯一键,默认值为00000000-0000-0000-0000-000000000000

I know that I can set the guid in my code: in the linq to sql model or elsewhere, or there is the default value in creating the sql server table (though this is overridden by the value generated in the code). 我知道我可以在我的代码中设置guid:在linq to sql模型或其他地方,或者在创建sql server表时有默认值(尽管这会被代码中生成的值覆盖)。 But where is best to put generate this key, noting that my tables are always going to change as my solution develops and therefore I shall regenerate my Linq to Sql model when it does. 但是最好放在哪里生成这个密钥,注意我的表总是会随着我的解决方案的发展而改变,因此我会重新生成我的Linq to Sql模型。

Does the same solution apply for a column to hold current datetime (of the insert), which would be updated with each update? 相同的解决方案是否适用于保存当前datetime (插入)的列,每次更新都会更新?

As you noted in you own post you can use the extensibility methods. 正如您在自己的帖子中所述,您可以使用可扩展性方法。 Adding to your post you can look at the partial methods created in the datacontext for inserting and updating of each table. 添加到您的帖子中,您可以查看在datacontext中创建的部分方法,以便插入和更新每个表。 Example with a table called "test" and a "changeDate"-column: 带有名为“test”和“changeDate”列的表的示例:

partial void InsertTest(Test instance)
{
    instance.idCol = System.Guid.NewGuid();
    this.ExecuteDynamicInsert(instance);
}

partial void UpdateTest(Test instance)
{
    instance.changeDate = DateTime.Now;
    this.ExecuteDynamicUpdate(instance);
}

Thanks, I've tried this out and it seems to work OK. 谢谢,我已经尝试过了,似乎工作正常。 I have another approach, which I think I shall use for guids: sqlserver default value to newid(), then in linqtosql set auto generated value property to true. 我有另一种方法,我认为我将用于guid:sqlserver默认值为newid(),然后在linqtosql中将auto generate generated属性设置为true。 This has to be done on each generation of the model, but this is fairly simple. 这必须在每一代模型上完成,但这很简单。

There's two things you can do: 你可以做两件事:

  • either just generate the GUID in your C# client side code and use that value 或者只是在C#客户端代码中生成GUID并使用该值
  • create a DEFAULT constraint on the GUID column in SQL Server that defaults to newid() for the column - the SQL Server will make SURE to always add a default - unless you specify a value yourself 在SQL Server中的GUID列上创建一个DEFAULT约束,该列默认为列的newid() - SQL Server将使SURE始终添加默认值 - 除非您自己指定值

As for the self-updating date/time columns - here you probably have to use either client-side logic to do that, or if you want to do it on SQL Server, you'll have to write a trigger. 至于自我更新日期/时间列 - 在这里你可能必须使用客户端逻辑来做到这一点,或者如果你想在SQL Server上执行它,你将不得不编写一个触发器。 That's really the only way to update a specific column everytime the row gets updated - there's no "autoupdate" constraint or anything like this (and the default constraint only work on INSERTs, not updates). 这是每次更新行时更新特定列的唯一方法 - 没有“自动更新”约束或类似的任何内容(默认约束仅适用于INSERT,而不是更新)。

Something like this might work: 像这样的东西可能会起作用:

CREATE TRIGGER TRG_UpdateDateTimestamp
ON (your table name)
AFTER UPDATE 
AS
    IF NOT UPDATE(DateTimeStamp)
    BEGIN
       UPDATE (yourtablename) 
       SET DateTimeStamp = GETDATE()
       WHERE EXISTS (SELECT * FROM inserted AS i 
                     WHERE i.OID = (yourtable).OID)
   END

Marc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM