Entity Framework Core seed data

Multi tool use
Multi tool use












1














So I have a Entity Framework Core first database and a .Net Core application.



I want to add Seed Data to it when creating the database. I know the FluentAPI has the HasData method but that is not what i am looking for.



When the database gets created I want to create a user with a random generated password (that is different every time) that will be saved on a file in the server.



I can't use the HasData method because then it will create the same password everytime.



So my question is: Is there a way, other than the HasData method, to add Seed data to a Entity Framework Core Code First database










share|improve this question





























    1














    So I have a Entity Framework Core first database and a .Net Core application.



    I want to add Seed Data to it when creating the database. I know the FluentAPI has the HasData method but that is not what i am looking for.



    When the database gets created I want to create a user with a random generated password (that is different every time) that will be saved on a file in the server.



    I can't use the HasData method because then it will create the same password everytime.



    So my question is: Is there a way, other than the HasData method, to add Seed data to a Entity Framework Core Code First database










    share|improve this question



























      1












      1








      1







      So I have a Entity Framework Core first database and a .Net Core application.



      I want to add Seed Data to it when creating the database. I know the FluentAPI has the HasData method but that is not what i am looking for.



      When the database gets created I want to create a user with a random generated password (that is different every time) that will be saved on a file in the server.



      I can't use the HasData method because then it will create the same password everytime.



      So my question is: Is there a way, other than the HasData method, to add Seed data to a Entity Framework Core Code First database










      share|improve this question















      So I have a Entity Framework Core first database and a .Net Core application.



      I want to add Seed Data to it when creating the database. I know the FluentAPI has the HasData method but that is not what i am looking for.



      When the database gets created I want to create a user with a random generated password (that is different every time) that will be saved on a file in the server.



      I can't use the HasData method because then it will create the same password everytime.



      So my question is: Is there a way, other than the HasData method, to add Seed data to a Entity Framework Core Code First database







      c# .net-core entity-framework-core






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 27 at 13:20

























      asked Dec 27 at 13:06









      kjwdamme

      334




      334
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Yes! There is a way! You can do as follows:



          public static class DatabaseInitializer
          {
          public static void Initialize(YourDbContext dbContext)
          {
          dbContext.Database.EnsureCreated();
          if (!dbContext.Users.Any())
          {
          // Write you necessary to code here to insert the User to database and save the the information to file.

          }

          }
          }


          The in the Configure method of the Startup class as follows:



          public void Configure(IApplicationBuilder app, IHostingEnvironment env, YourDbContext dbContext)
          {
          // other configurations
          ..............

          DatabaseInitializer.Initialize(dbContext);

          ..............
          // other configurations
          }


          Initialize method will be called only once during the application start up.






          share|improve this answer

















          • 1




            I didn't see ASP.NET Core mentioned anywhere.
            – Imantas
            Dec 27 at 13:54










          • @Imantas Did not understand your comment!
            – TanvirArjel
            Dec 27 at 13:55










          • My point is the question author didn't mention ASP.NET Core anywhere in the question while your code is designed to be used within ASP.NET Core. But probably you guessed it right. Nevermind.
            – Imantas
            Dec 27 at 14:03










          • Oh! Understood! I have guessed from his question tags! :) Thank you.
            – TanvirArjel
            Dec 27 at 14:05










          • I did mention .Net Core in the question. Thanks for the answer!
            – kjwdamme
            Dec 27 at 14:26



















          0














          Create a DataSeeder class and Inject DbContext in it.
          Write a seedData method to insert the required data. This would be custom logic.



          You can run this code after database is created.



          public class DataSeeder
          {
          public static void SeedRandomPassword(YourDbContext context)
          {
          //// your custom logic to generate random password
          //// set it to right user


          context.Users.Add(); // Or Update depending on what you need
          context.SaveChanges();
          }
          }
          }





          share|improve this answer








          New contributor




          Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















            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%2f53945596%2fentity-framework-core-seed-data%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Yes! There is a way! You can do as follows:



            public static class DatabaseInitializer
            {
            public static void Initialize(YourDbContext dbContext)
            {
            dbContext.Database.EnsureCreated();
            if (!dbContext.Users.Any())
            {
            // Write you necessary to code here to insert the User to database and save the the information to file.

            }

            }
            }


            The in the Configure method of the Startup class as follows:



            public void Configure(IApplicationBuilder app, IHostingEnvironment env, YourDbContext dbContext)
            {
            // other configurations
            ..............

            DatabaseInitializer.Initialize(dbContext);

            ..............
            // other configurations
            }


            Initialize method will be called only once during the application start up.






            share|improve this answer

















            • 1




              I didn't see ASP.NET Core mentioned anywhere.
              – Imantas
              Dec 27 at 13:54










            • @Imantas Did not understand your comment!
              – TanvirArjel
              Dec 27 at 13:55










            • My point is the question author didn't mention ASP.NET Core anywhere in the question while your code is designed to be used within ASP.NET Core. But probably you guessed it right. Nevermind.
              – Imantas
              Dec 27 at 14:03










            • Oh! Understood! I have guessed from his question tags! :) Thank you.
              – TanvirArjel
              Dec 27 at 14:05










            • I did mention .Net Core in the question. Thanks for the answer!
              – kjwdamme
              Dec 27 at 14:26
















            1














            Yes! There is a way! You can do as follows:



            public static class DatabaseInitializer
            {
            public static void Initialize(YourDbContext dbContext)
            {
            dbContext.Database.EnsureCreated();
            if (!dbContext.Users.Any())
            {
            // Write you necessary to code here to insert the User to database and save the the information to file.

            }

            }
            }


            The in the Configure method of the Startup class as follows:



            public void Configure(IApplicationBuilder app, IHostingEnvironment env, YourDbContext dbContext)
            {
            // other configurations
            ..............

            DatabaseInitializer.Initialize(dbContext);

            ..............
            // other configurations
            }


            Initialize method will be called only once during the application start up.






            share|improve this answer

















            • 1




              I didn't see ASP.NET Core mentioned anywhere.
              – Imantas
              Dec 27 at 13:54










            • @Imantas Did not understand your comment!
              – TanvirArjel
              Dec 27 at 13:55










            • My point is the question author didn't mention ASP.NET Core anywhere in the question while your code is designed to be used within ASP.NET Core. But probably you guessed it right. Nevermind.
              – Imantas
              Dec 27 at 14:03










            • Oh! Understood! I have guessed from his question tags! :) Thank you.
              – TanvirArjel
              Dec 27 at 14:05










            • I did mention .Net Core in the question. Thanks for the answer!
              – kjwdamme
              Dec 27 at 14:26














            1












            1








            1






            Yes! There is a way! You can do as follows:



            public static class DatabaseInitializer
            {
            public static void Initialize(YourDbContext dbContext)
            {
            dbContext.Database.EnsureCreated();
            if (!dbContext.Users.Any())
            {
            // Write you necessary to code here to insert the User to database and save the the information to file.

            }

            }
            }


            The in the Configure method of the Startup class as follows:



            public void Configure(IApplicationBuilder app, IHostingEnvironment env, YourDbContext dbContext)
            {
            // other configurations
            ..............

            DatabaseInitializer.Initialize(dbContext);

            ..............
            // other configurations
            }


            Initialize method will be called only once during the application start up.






            share|improve this answer












            Yes! There is a way! You can do as follows:



            public static class DatabaseInitializer
            {
            public static void Initialize(YourDbContext dbContext)
            {
            dbContext.Database.EnsureCreated();
            if (!dbContext.Users.Any())
            {
            // Write you necessary to code here to insert the User to database and save the the information to file.

            }

            }
            }


            The in the Configure method of the Startup class as follows:



            public void Configure(IApplicationBuilder app, IHostingEnvironment env, YourDbContext dbContext)
            {
            // other configurations
            ..............

            DatabaseInitializer.Initialize(dbContext);

            ..............
            // other configurations
            }


            Initialize method will be called only once during the application start up.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 27 at 13:34









            TanvirArjel

            3,75821538




            3,75821538








            • 1




              I didn't see ASP.NET Core mentioned anywhere.
              – Imantas
              Dec 27 at 13:54










            • @Imantas Did not understand your comment!
              – TanvirArjel
              Dec 27 at 13:55










            • My point is the question author didn't mention ASP.NET Core anywhere in the question while your code is designed to be used within ASP.NET Core. But probably you guessed it right. Nevermind.
              – Imantas
              Dec 27 at 14:03










            • Oh! Understood! I have guessed from his question tags! :) Thank you.
              – TanvirArjel
              Dec 27 at 14:05










            • I did mention .Net Core in the question. Thanks for the answer!
              – kjwdamme
              Dec 27 at 14:26














            • 1




              I didn't see ASP.NET Core mentioned anywhere.
              – Imantas
              Dec 27 at 13:54










            • @Imantas Did not understand your comment!
              – TanvirArjel
              Dec 27 at 13:55










            • My point is the question author didn't mention ASP.NET Core anywhere in the question while your code is designed to be used within ASP.NET Core. But probably you guessed it right. Nevermind.
              – Imantas
              Dec 27 at 14:03










            • Oh! Understood! I have guessed from his question tags! :) Thank you.
              – TanvirArjel
              Dec 27 at 14:05










            • I did mention .Net Core in the question. Thanks for the answer!
              – kjwdamme
              Dec 27 at 14:26








            1




            1




            I didn't see ASP.NET Core mentioned anywhere.
            – Imantas
            Dec 27 at 13:54




            I didn't see ASP.NET Core mentioned anywhere.
            – Imantas
            Dec 27 at 13:54












            @Imantas Did not understand your comment!
            – TanvirArjel
            Dec 27 at 13:55




            @Imantas Did not understand your comment!
            – TanvirArjel
            Dec 27 at 13:55












            My point is the question author didn't mention ASP.NET Core anywhere in the question while your code is designed to be used within ASP.NET Core. But probably you guessed it right. Nevermind.
            – Imantas
            Dec 27 at 14:03




            My point is the question author didn't mention ASP.NET Core anywhere in the question while your code is designed to be used within ASP.NET Core. But probably you guessed it right. Nevermind.
            – Imantas
            Dec 27 at 14:03












            Oh! Understood! I have guessed from his question tags! :) Thank you.
            – TanvirArjel
            Dec 27 at 14:05




            Oh! Understood! I have guessed from his question tags! :) Thank you.
            – TanvirArjel
            Dec 27 at 14:05












            I did mention .Net Core in the question. Thanks for the answer!
            – kjwdamme
            Dec 27 at 14:26




            I did mention .Net Core in the question. Thanks for the answer!
            – kjwdamme
            Dec 27 at 14:26













            0














            Create a DataSeeder class and Inject DbContext in it.
            Write a seedData method to insert the required data. This would be custom logic.



            You can run this code after database is created.



            public class DataSeeder
            {
            public static void SeedRandomPassword(YourDbContext context)
            {
            //// your custom logic to generate random password
            //// set it to right user


            context.Users.Add(); // Or Update depending on what you need
            context.SaveChanges();
            }
            }
            }





            share|improve this answer








            New contributor




            Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.























              0














              Create a DataSeeder class and Inject DbContext in it.
              Write a seedData method to insert the required data. This would be custom logic.



              You can run this code after database is created.



              public class DataSeeder
              {
              public static void SeedRandomPassword(YourDbContext context)
              {
              //// your custom logic to generate random password
              //// set it to right user


              context.Users.Add(); // Or Update depending on what you need
              context.SaveChanges();
              }
              }
              }





              share|improve this answer








              New contributor




              Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                0












                0








                0






                Create a DataSeeder class and Inject DbContext in it.
                Write a seedData method to insert the required data. This would be custom logic.



                You can run this code after database is created.



                public class DataSeeder
                {
                public static void SeedRandomPassword(YourDbContext context)
                {
                //// your custom logic to generate random password
                //// set it to right user


                context.Users.Add(); // Or Update depending on what you need
                context.SaveChanges();
                }
                }
                }





                share|improve this answer








                New contributor




                Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                Create a DataSeeder class and Inject DbContext in it.
                Write a seedData method to insert the required data. This would be custom logic.



                You can run this code after database is created.



                public class DataSeeder
                {
                public static void SeedRandomPassword(YourDbContext context)
                {
                //// your custom logic to generate random password
                //// set it to right user


                context.Users.Add(); // Or Update depending on what you need
                context.SaveChanges();
                }
                }
                }






                share|improve this answer








                New contributor




                Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered Dec 27 at 14:08









                Manoj Choudhari

                112




                112




                New contributor




                Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Manoj Choudhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53945596%2fentity-framework-core-seed-data%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







                    36 CQ,vWIkIpKv3cNyuOip2SM5y,iY hJD rinmZquBBdfNeCLX5SyW86ho0T BM,C9CmFbg0d,XvgfS4 dh4nwO3qWHOkVGEP
                    tt,wyjt3Ej xKN1R

                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas