简体   繁体   English

Webforms上的主键(最初加载还是保存)?

[英]Primary keys on webforms (load initially or on save)?

This is just a general question irrespective of database architecture. 无论数据库的体系结构如何,这只是一个普遍的问题。 I am maintaining an ASP.NET web application. 我正在维护一个ASP.NET Web应用程序。 The structure is such that, 结构是这样的,

Say on 'Add a new employee' webform “添加新员工”网络表单

  • The primary key (or the record id to be saved with) is initially loaded on form load event & displayed as a label 主键(或要保存的记录ID)最初在表单加载事件中加载并显示为标签
    • So when the form loads, the record id to save with is shown to the user 因此,当表单加载时,要保存的记录ID会显示给用户

Positives: 正面:

  • End user already knows what the id/serial of the form is (even before he saves the form) 最终用户已经知道表单的ID /序列号是什么(甚至在保存表单之前)
  • So on form save when he is directed to gridview screen (with all entries) he can search records easily (although the most recent one is at the top anyway) 因此,在保存表单时,当他被定向到Gridview屏幕(包含所有条目)时,他可以轻松地搜索记录(尽管最近的记录始终位于顶部)

Negatives: 劣势:

  • If he does not save the form, say he just cancels after loading the data entry form, the id/key initially fetched is wasted (in my case it is a sequence field fetched on form load from database) 如果他不保存表单,比如说他只是在加载数据输入表单后取消,那么最初获取的id /键就被浪费了(在我的情况下,这是从数据库加载表单时获取的序列字段)

What do you guys do in these scenarios ? 你们在这些情况下会做什么? Which approach would you recommend for 'web applications' ? 您将为“ Web应用程序”推荐哪种方法? And how to facilitate the user with a different approach ? 以及如何以不同的方式方便用户? Is our current approach recommended (To me,it wastes the ids/sequence from database) 是否建议使用我们当前的方法(对我来说,这浪费了数据库中的ID /序列)

I'd always recommend not presenting the identity field value for the record being created until the record has been created . 我总是建议不要在创建记录之前不提供正在创建的记录的标识字段值。 The "create a temporary placeholder record first to obtain the identity field value ahead of time" approach can, as you mention, result in wasted IDs, unless you have a process in place to reclaim them. 如您提到的,“首先创建临时占位符记录以提前获取标识字段值”方法可能会导致浪费ID,除非您有适当的过程来回收它们。

You can always pop-up a message box when the user presses save that tells them the identity field value of the newly created record. 当用户按下保存时,您总是可以弹出一个消息框,告诉他们新创建的记录的标识字段值。

In this situation you could use a GUID created by the application itself. 在这种情况下,您可以使用由应用程序本身创建的GUID。 The database would then only have the PK set to be a Unique Identifier (GUID) and that it must not be null. 然后,数据库将仅将PK设置为唯一标识符(GUID),并且它不能为null。 In this situation you are not wasting any unique keys as each call to get a new GUID should be definition produce a (mathmatically) unique identifier. 在这种情况下,您不会浪费任何唯一键,因为每次获取新GUID的调用都应定义为生成(数学上)唯一的标识符。 It is worth noting that if you use this method, it is best to make sure your PK is not set up to be clustered. 值得注意的是,如果使用此方法,则最好确保未将PK设置为集群。 The resulting index reorganisation upon insert could quickly result in an application that suffers performance hits. 插入后产生的索引重组可能很快导致应用程序性能下降。

For one: I wouldn't care so much about wasted id values. 例如:我不会在乎浪费的id值。 When you are in danger of running out of int32 values (and when has that happened to you last?), use int64. 当您有可能用尽int32值的危险时(最后一次发生这种情况是什么?),请使用int64。 The user experience is way much more important than wasting a few id values. 用户体验比浪费一些id值重要得多。

Having said that, I would not want the primary key to be anything the user would want to type in. If you are having a primary key that users need to type in, chances are it then is (or will be requested to be) more than just an int32/64 value and carries (will carry) meaning in its composition and/or formatting. 话虽如此,我不希望主键是用户想要键入的任何内容。如果您拥有用户需要输入的主键,那么可能(或将要求输入)更多不仅仅是一个int32 / 64值,并且在其组成和/或格式中带有(将带有)含义。 Primary keys should not have that. 主键不应具有该功能。 (Tons of reasons google for meaningless primary keys or other such terms). (出于各种原因,谷歌搜索了毫无意义的主键或其他类似术语)。

If you need a meaningful key, make it a secondary index that is in no way related to the primary key. 如果您需要一个有意义的键,则使其成为与主键完全无关的二级索引。 If a part of that is still a sequential number taken from some counter value in your database. 如果其中一部分仍然是从数据库中某个计数器值获取的序列号。 Decide whether functionally it is a problem for gaps to appear in the sequence. 确定在功能上是否在序列中出现缺口是一个问题。 (The tax people generally don't want gaps in invoice numbers). (税务人员通常不希望发票号有空白)。 If functionally it is no problem, then certainly don't start worrying about it technically. 如果在功能上没有问题,那么一定不要从技术上开始担心它。 If functionally it is a problem, then yes, you have no option but to wait for the save in order to show it to the user. 如果从功能上来说是个问题,那么是的,您别无选择,只能等待保存才能显示给用户。 But, please, when you do, don't do it in a popup. 但是,请不要在弹出窗口中这样做。 They are horribly intrusive as they have to be dismissed. 他们非常令人讨厌,因为他们不得不被解雇。 Just put up an informative message on the screen where the user is sent after (s)he saves the new employee. 只需在保存新员工之后在屏幕上显示一条提示性消​​息,向用户发送该消息。 Much like gmail is telling you about actions you have performed just above the list of messages. 就像gmail会告诉您有关您在邮件列表上方执行的操作一样。

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

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