Custom sequence number generator in EF Core using Cosmos DB provider












0















I have the requirement to generate a unique identifier per record of a given entity. I'm using Cosmos DB, where one collection can hold multiple document types (entities).
Using EF Core, Cosmos DB provider, I update these collections with new/updated data, all running smoothly where EF Core generates nice GUIDs for the ID fields of my entities.



Now, because some entities are involved in communication to a customer, I need a way to generate 'friendly' unique numbers per entity. An example is an account number that follows a specific format, e.g. YYYY-###### (2019-000001 for example).



This format is defined per entity type, and the uniqueness is:
- per entity type (Which is a field in the document)
- per collection



How can I configure EF Core to use a custom number format generator when saving the entity? One approach I was thinking of, was creating a UDF in Cosmos, that generates the number based on the format, but I'm afraid that I would need to requery the document after saving, as EF Core will have no clue about the value of that field.










share|improve this question



























    0















    I have the requirement to generate a unique identifier per record of a given entity. I'm using Cosmos DB, where one collection can hold multiple document types (entities).
    Using EF Core, Cosmos DB provider, I update these collections with new/updated data, all running smoothly where EF Core generates nice GUIDs for the ID fields of my entities.



    Now, because some entities are involved in communication to a customer, I need a way to generate 'friendly' unique numbers per entity. An example is an account number that follows a specific format, e.g. YYYY-###### (2019-000001 for example).



    This format is defined per entity type, and the uniqueness is:
    - per entity type (Which is a field in the document)
    - per collection



    How can I configure EF Core to use a custom number format generator when saving the entity? One approach I was thinking of, was creating a UDF in Cosmos, that generates the number based on the format, but I'm afraid that I would need to requery the document after saving, as EF Core will have no clue about the value of that field.










    share|improve this question

























      0












      0








      0








      I have the requirement to generate a unique identifier per record of a given entity. I'm using Cosmos DB, where one collection can hold multiple document types (entities).
      Using EF Core, Cosmos DB provider, I update these collections with new/updated data, all running smoothly where EF Core generates nice GUIDs for the ID fields of my entities.



      Now, because some entities are involved in communication to a customer, I need a way to generate 'friendly' unique numbers per entity. An example is an account number that follows a specific format, e.g. YYYY-###### (2019-000001 for example).



      This format is defined per entity type, and the uniqueness is:
      - per entity type (Which is a field in the document)
      - per collection



      How can I configure EF Core to use a custom number format generator when saving the entity? One approach I was thinking of, was creating a UDF in Cosmos, that generates the number based on the format, but I'm afraid that I would need to requery the document after saving, as EF Core will have no clue about the value of that field.










      share|improve this question














      I have the requirement to generate a unique identifier per record of a given entity. I'm using Cosmos DB, where one collection can hold multiple document types (entities).
      Using EF Core, Cosmos DB provider, I update these collections with new/updated data, all running smoothly where EF Core generates nice GUIDs for the ID fields of my entities.



      Now, because some entities are involved in communication to a customer, I need a way to generate 'friendly' unique numbers per entity. An example is an account number that follows a specific format, e.g. YYYY-###### (2019-000001 for example).



      This format is defined per entity type, and the uniqueness is:
      - per entity type (Which is a field in the document)
      - per collection



      How can I configure EF Core to use a custom number format generator when saving the entity? One approach I was thinking of, was creating a UDF in Cosmos, that generates the number based on the format, but I'm afraid that I would need to requery the document after saving, as EF Core will have no clue about the value of that field.







      c# entity-framework asp.net-core azure-cosmosdb azure-cosmosdb-sqlapi






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 12:44









      MortanaMortana

      4611518




      4611518
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Maybe instead you can add a field with your custom generated id, add an index on it in CosmosDB and use it instead. Such a solution will be same fast and you won't have problems with EF Core.



          The second option is to use a string instead of Guid and manually create an id. But I'm not sure if EF Core already supports such mapping. You can try to do this in protected override void OnModelCreating(ModelBuilder modelBuilder)



          The last option which comes to my mind is to create Guid that will fulfil your requirements and starts (or contains) required number. Like: 20190000-2629-478a-be9e-bc596f2e85c2. But I'm not sure if it is a good idea






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54254300%2fcustom-sequence-number-generator-in-ef-core-using-cosmos-db-provider%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Maybe instead you can add a field with your custom generated id, add an index on it in CosmosDB and use it instead. Such a solution will be same fast and you won't have problems with EF Core.



            The second option is to use a string instead of Guid and manually create an id. But I'm not sure if EF Core already supports such mapping. You can try to do this in protected override void OnModelCreating(ModelBuilder modelBuilder)



            The last option which comes to my mind is to create Guid that will fulfil your requirements and starts (or contains) required number. Like: 20190000-2629-478a-be9e-bc596f2e85c2. But I'm not sure if it is a good idea






            share|improve this answer




























              0














              Maybe instead you can add a field with your custom generated id, add an index on it in CosmosDB and use it instead. Such a solution will be same fast and you won't have problems with EF Core.



              The second option is to use a string instead of Guid and manually create an id. But I'm not sure if EF Core already supports such mapping. You can try to do this in protected override void OnModelCreating(ModelBuilder modelBuilder)



              The last option which comes to my mind is to create Guid that will fulfil your requirements and starts (or contains) required number. Like: 20190000-2629-478a-be9e-bc596f2e85c2. But I'm not sure if it is a good idea






              share|improve this answer


























                0












                0








                0







                Maybe instead you can add a field with your custom generated id, add an index on it in CosmosDB and use it instead. Such a solution will be same fast and you won't have problems with EF Core.



                The second option is to use a string instead of Guid and manually create an id. But I'm not sure if EF Core already supports such mapping. You can try to do this in protected override void OnModelCreating(ModelBuilder modelBuilder)



                The last option which comes to my mind is to create Guid that will fulfil your requirements and starts (or contains) required number. Like: 20190000-2629-478a-be9e-bc596f2e85c2. But I'm not sure if it is a good idea






                share|improve this answer













                Maybe instead you can add a field with your custom generated id, add an index on it in CosmosDB and use it instead. Such a solution will be same fast and you won't have problems with EF Core.



                The second option is to use a string instead of Guid and manually create an id. But I'm not sure if EF Core already supports such mapping. You can try to do this in protected override void OnModelCreating(ModelBuilder modelBuilder)



                The last option which comes to my mind is to create Guid that will fulfil your requirements and starts (or contains) required number. Like: 20190000-2629-478a-be9e-bc596f2e85c2. But I'm not sure if it is a good idea







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 18 at 13:52









                Piotr StappPiotr Stapp

                14k44787




                14k44787






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54254300%2fcustom-sequence-number-generator-in-ef-core-using-cosmos-db-provider%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Homophylophilia

                    Updating UILabel text programmatically using a function

                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage