---
title: "How to seed data in .NET Core 6 with Entity Framework?"  
description: "How to seed data in .NET Core 6 with Entity Framework?"  
author: "Sandra Emily"  
published: 2023-11-07  
updated: 2023-11-08  
canonical: https://www.mindstick.com/forum/160451/how-to-seed-data-in-dot-net-core-6-with-entity-framework  
category: ".net core"  
tags: ["entity framework", "asp.net core", ".net core", ".net core 6"]  
reading_time: 2 minutes  

---

# How to seed data in .NET Core 6 with Entity Framework?

How to [seed](https://yourviews.mindstick.com/story/4868/9-reasons-why-pumpkin-seed-is-a-superfood) [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) in .NET Core 6 with [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach)?

## Replies

### Reply by Aryan Kumar

In .NET Core 6 with [Entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library), you can seed data into your database during application startup. This is commonly done to initialize the database with default or sample data. To seed data, follow these steps:

**Create a Class for Seeding Data**: Start by creating a class that will contain the data you want to seed. This class can be placed anywhere in your project. For example:

```plaintext
public class SeedData
{
    public static void Initialize(IServiceProvider serviceProvider)
    {
        using (var context = new YourDbContext(serviceProvider.GetRequiredService<DbContextOptions<YourDbContext>>()))
        {
            // Check if data already exists
            if (context.YourEntities.Any())
            {
                return; // Data has already been seeded
            }

            // Seed your data here
            var initialData = new List<YourEntity>
            {
                new YourEntity { Property1 = "Value1", Property2 = "Value2" },
                new YourEntity { Property1 = "Value3", Property2 = "Value4" },
                // Add more data as needed
            };

            context.YourEntities.AddRange(initialData);
            context.SaveChanges();
        }
    }
}
```

In this example, **SeedData** is a class containing a static **Initialize** method that seeds data into the database. Replace **YourDbContext** with the name of your database context, and **YourEntity** with the name of your entity class.

**Call the SeedData Method in Program.cs**: In your **Program.cs** file, call the **SeedData.Initialize** method to seed data during application startup. You can do this inside the **Main** method before running the application:

```plaintext
public class Program
{
    public static void Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();

        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;
            try
            {
                SeedData.Initialize(services);
            }
            catch (Exception ex)
            {
                var logger = services.GetRequiredService<ILogger<Program>>();
                logger.LogError(ex, "An error occurred while seeding the database.");
            }
        }

        host.Run();
    }

    // Rest of the code
}
```

**Run the Application**: When you run your application, the **SeedData.Initialize** method will be called during application startup. It will check if data already exists in the database and seed the data only if it's not already present.

Make sure to replace **YourDbContext** with the name of your database context, and **YourEntity** with the name of your entity class. Also, customize the data you want to seed in the **initialData** list as needed.

By following these steps, you can seed data into your database when your .NET Core 6 application starts, ensuring that the database is initialized with the desired data.


---

Original Source: https://www.mindstick.com/forum/160451/how-to-seed-data-in-dot-net-core-6-with-entity-framework

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
