Entity Framework Core seed data

Multi tool use
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
add a comment |
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
add a comment |
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
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
c# .net-core entity-framework-core
edited Dec 27 at 13:20
asked Dec 27 at 13:06
kjwdamme
334
334
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
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
|
show 3 more comments
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();
}
}
}
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
|
show 3 more comments
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.
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
|
show 3 more comments
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.
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.
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
|
show 3 more comments
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
|
show 3 more comments
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();
}
}
}
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.
add a comment |
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();
}
}
}
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.
add a comment |
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();
}
}
}
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();
}
}
}
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.
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.
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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