<bdo id='QhYbz'></bdo><ul id='QhYbz'></ul>

    <tfoot id='QhYbz'></tfoot>

    <i id='QhYbz'><tr id='QhYbz'><dt id='QhYbz'><q id='QhYbz'><span id='QhYbz'><b id='QhYbz'><form id='QhYbz'><ins id='QhYbz'></ins><ul id='QhYbz'></ul><sub id='QhYbz'></sub></form><legend id='QhYbz'></legend><bdo id='QhYbz'><pre id='QhYbz'><center id='QhYbz'></center></pre></bdo></b><th id='QhYbz'></th></span></q></dt></tr></i><div id='QhYbz'><tfoot id='QhYbz'></tfoot><dl id='QhYbz'><fieldset id='QhYbz'></fieldset></dl></div>
  1. <legend id='QhYbz'><style id='QhYbz'><dir id='QhYbz'><q id='QhYbz'></q></dir></style></legend>
  2. <small id='QhYbz'></small><noframes id='QhYbz'>

    1. 使用 Max+1 创建用户友好的唯一序列号的替代方法

      Alternatives to using Max+1 to create a user friendly unique sequence numbers(使用 Max+1 创建用户友好的唯一序列号的替代方法)

        <small id='ZcX2M'></small><noframes id='ZcX2M'>

        <legend id='ZcX2M'><style id='ZcX2M'><dir id='ZcX2M'><q id='ZcX2M'></q></dir></style></legend>
      1. <tfoot id='ZcX2M'></tfoot>
          <bdo id='ZcX2M'></bdo><ul id='ZcX2M'></ul>

                <tbody id='ZcX2M'></tbody>

                <i id='ZcX2M'><tr id='ZcX2M'><dt id='ZcX2M'><q id='ZcX2M'><span id='ZcX2M'><b id='ZcX2M'><form id='ZcX2M'><ins id='ZcX2M'></ins><ul id='ZcX2M'></ul><sub id='ZcX2M'></sub></form><legend id='ZcX2M'></legend><bdo id='ZcX2M'><pre id='ZcX2M'><center id='ZcX2M'></center></pre></bdo></b><th id='ZcX2M'></th></span></q></dt></tr></i><div id='ZcX2M'><tfoot id='ZcX2M'></tfoot><dl id='ZcX2M'><fieldset id='ZcX2M'></fieldset></dl></div>
                本文介绍了使用 Max+1 创建用户友好的唯一序列号的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的 asp.net mvc web 应用程序中有存储库方法,可以自动生成一个唯一的序列号,称为 Tag:-

                I have the repository method inside my asp.net mvc web application, to automatically generate a unique sequence number called Tag:-

                public void InsertOrUpdateServer(TMSServer server, string username)
                        {
                
                    if (server.TMSServerID == default(int))
                            {
                                // New entity
                    int technologyypeID = GetTechnologyTypeID("Server");
                    Technology technology = new Technology
                       {
                     IsDeleted = true,
                     Tag = "S" + GetTagMaximumeNumber(technologyypeID).ToString(),
                     StartDate = DateTime.Now,
                     };
                   Save();
                //code goes here
                

                GetTagMaximumeNumber() 函数在哪里:-

                Where the GetTagMaximumeNumber() function is :-

                public int GetTagMaximumeNumber(int typeID) {
                string s = tms.Technologies.Where(a => a.TypeID == typeID).Max(a2 => a2.Tag.Substring(1));
                
                           if (s != null)
                           {
                             return Int32.Parse(s) + 1;
                
                           }
                           else {
                               return 100;//if this is the first record created
                           }}
                

                上面会得到标签的最大个数并加1然后尝试保存到数据库中.以上运行良好,但我确信在多个用户将添加记录的生产服务器上,将生成相同的标签号,并且在尝试添加数据库中已经存在的自动生成的标签号时会引发异常.所以我有以下问题:-1

                The above will get the maximum number of the tag and add 1 then try to save it into the database. The above is working well, but I am sure that on production server where multiple users will be adding records, same tag numbers will be generated and exception will be raised when trying to add an automatically generated tag number that already exists on the database. So I have the following questions:-1

                1. 我可以遵循哪些其他方法来获得始终唯一的序列号.

                1. What are the other approaches which I can follow to have a sequence number that is always unique.

                在我目前的方法中,标签号的差距很小.仅当用户删除记录并且其标签号不是最大值时才会出现差距.那么有没有办法在任何新方法中仍然保持这一点,从而使差距最小?

                In my current approach the gaps in the Tag number are minimal. The gap only occurs when user delete a record and its tag number was not the maximum. So is there a way to still have this in any new approach, so gaps will be minimal?

                感谢您的帮助.

                推荐答案

                如果 SQL 2012 中的新序列不是一个选项(因为您无法升级),您可以创建一个存储最后序列号的表.

                If the new sequences in SQL 2012 are not an option (because you can't upgrade) you could create a table that stores the last sequence numbers.

                p>

                在 EF 上下文中,表示类看起来像

                In the EF context the representing class would look like

                class TaggedSequence
                {
                    [Key]
                    public int Id { get; set; }
                
                    [Required]
                    public int Last { get; set; }
                
                    [Required]
                    [Column(TypeName = "CHAR"), StringLength(1)]
                    public string Tag { get; set; }
                
                    [Timestamp]
                    public byte[] Rowversion { get; set; }
                }
                

                以及使用它的准系统程序:

                And the barebone procedure to use it:

                using (var context = new MyContext())
                {
                    try
                    {
                        var tag = "S";
                        var sequence = context.TaggedSequence.Single(s => s.Tag == tag);
                        sequence.Last += 1;
                        Technology technology = new Technology { ... };
                        technology.Id = tag + sequence.Last;
                        context.Technologies.Add(technology);
                        context.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        // Retry
                    }
                }
                

                (您必须将其放入您的存储库对象中)

                (You'll have to fit it in into your repository object)

                并发检查是为了防止并发用户绘制相同的数字.Rowversion 确保仅当其值在获取记录和更新记录之间没有改变时才允许更新.

                The concurrency check is there to prevent concurrent users form drawing the same number. The Rowversion ensures that updates are only allowed if its value did not change between fetching the record and updating it.

                这篇关于使用 Max+1 创建用户友好的唯一序列号的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                    <tbody id='pyaEW'></tbody>

                      • <bdo id='pyaEW'></bdo><ul id='pyaEW'></ul>
                      • <legend id='pyaEW'><style id='pyaEW'><dir id='pyaEW'><q id='pyaEW'></q></dir></style></legend>

                        <small id='pyaEW'></small><noframes id='pyaEW'>

                          <i id='pyaEW'><tr id='pyaEW'><dt id='pyaEW'><q id='pyaEW'><span id='pyaEW'><b id='pyaEW'><form id='pyaEW'><ins id='pyaEW'></ins><ul id='pyaEW'></ul><sub id='pyaEW'></sub></form><legend id='pyaEW'></legend><bdo id='pyaEW'><pre id='pyaEW'><center id='pyaEW'></center></pre></bdo></b><th id='pyaEW'></th></span></q></dt></tr></i><div id='pyaEW'><tfoot id='pyaEW'></tfoot><dl id='pyaEW'><fieldset id='pyaEW'></fieldset></dl></div>

                          <tfoot id='pyaEW'></tfoot>