---
title: "Migrating to Entity Framework Core Seed Data - Muhammad Rehan Saeed"
description: "How to migrate an existing database to use Entity Framework Core 2.1 Seed Data to insert static data into your tables while using migrations."
canonical_url: "https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/"
last_updated: "2018-07-01T00:00:00+00:00"
meta:
  author: "Muhammad Rehan Saeed"
  keywords: "Entity Framework Core,Migrations,Seed Data"
  "og:description": "How to migrate an existing database to use Entity Framework Core 2.1 Seed Data to insert static data into your tables while using migrations."
  "og:image": "https://rehansaeed.com/_og/s/o_4fa13ea186b10db7.png"
  "og:image:alt": "Migrating to Entity Framework Core Seed Data - Muhammad Rehan Saeed"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "Migrating to Entity Framework Core Seed Data"
  "twitter:image": "https://rehansaeed.com/_og/s/o_4fa13ea186b10db7.png"
  "twitter:image:alt": "Migrating to Entity Framework Core Seed Data - Muhammad Rehan Saeed"
---

> Index of every page on this site: [llms.txt](https://rehansaeed.com/llms.txt)

# [Migrating to Entity Framework Core Seed Data](https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/)

Posted 1 July 2018 - **2 min read**

![Microsoft .NET](https://rehansaeed.com/_ipx/f_png&s_2560x1440/images/hero/Microsoft-.NET-1366x768.png)

I was already using Entity Framework Core 2.0 and had written some custom code to enter some static seed data to certain tables. Entity Framework 2.1 added support for [data seeding](https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding) which manages your seed data for you and adds them to your Entity Framework Core migrations.

The problem is that if you've already got data in your tables, when you add a migration containing seed data, you will get exceptions thrown as Entity Framework tries to insert data that is already there. Entity Framework is naive, it assumes that it is the only thing editing the database.

Migrating to using data seeding requires a few extra steps that aren't documented anywhere and weren't obvious to me. Lets walk through an example. Assuming we have the following model and database context:

```cs
public class Car
{
    public int CarId { get; set; }

    public string Make { get; set; }

    public string Model { get; set; }
}

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions options)
        : base(options)
    {
    }

    public DbSet<Car> Cars { get; set; }
}
```

We can add some seed data by overriding the `OnModelCreating` method on our database context class. You need to make sure your seed data matches the existing data in your database.

```cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Car>().HasData(
        new Car() { CarId = 1, Make = "Ferrari", Model = "F40" },
        new Car() { CarId = 2, Make = "Ferrari", Model = "F50" },
        new Car() { CarId = 3, Make = "Lambourghini", Model = "Countach" });
}
```

If we run a command to add a database migration, the generated code looks like this:

```powershell
dotnet ef migrations add AddSeedData
```

```cs
public partial class AddSeedData : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.InsertData(
            table: "Cars",
            columns: new[] { "CarId", "Make", "Model" },
            values: new object[] { 1, "Ferrari", "F40" });

        migrationBuilder.InsertData(
            table: "Cars",
            columns: new[] { "CarId", "Make", "Model" },
            values: new object[] { 2, "Ferrari", "F50" });

        migrationBuilder.InsertData(
            table: "Cars",
            columns: new[] { "CarId", "Make", "Model" },
            values: new object[] { 3, "Lambourghini", "Countach" });
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DeleteData(
            table: "Cars",
            keyColumn: "CarId",
            keyValue: 1);

        migrationBuilder.DeleteData(
            table: "Cars",
            keyColumn: "CarId",
            keyValue: 2);

        migrationBuilder.DeleteData(
            table: "Cars",
            keyColumn: "CarId",
            keyValue: 3);
    }
}
```

This is what you need to do:

1. Comment out all of the `InsertData` lines in the generated migration.
2. Run the migration on your database containing the existing seed data. This is effectively doing a null operation but records the fact that the `AddSeedData` migration has been run.
3. Uncomment the `InsertData` lines in the generated migration so that if you run the migrations on a fresh database, seed data still gets added. For your existing databases, since the migration has already been run on them, they will not add the seed data twice.

That's it, hope that helps someone.

## Web Mentions

[What's this?](https://en.wikipedia.org/wiki/Webmention)

### 0 Linked

### 0 Replies

## GitHub Comments

### 10 Comments

[Comment on GitHub](https://github.com/RehanSaeed/rehansaeed.github.io/issues/88)

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![MartÃ­n Coll](https://camo.githubusercontent.com/3dea7115a319f6aecc0ab598135bbace93eaf48d3d5ff91ea4541c64b5854e9a/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f65373530636631363532653833383130316636303631613539336336646131343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/3dea7115a319f6aecc0ab598135bbace93eaf48d3d5ff91ea4541c64b5854e9a/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f65373530636631363532653833383130316636303631613539336336646131343f733d393626643d6d6d26723d67) **MartÃ­n Coll** commented on **2018-07-02 01:01:09**

Yep, seed data works the same as the rest of your model now. The process you describe is exactly the same you'd do if you had manually changed the schema AND THEN decided to write the model classes and generating the migration.

Thanks for taking the time to understand it and blog about it!

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2018-07-02 09:01:54**

> Yep, seed data works the same as the rest of your model now. The process you describe is exactly the same you'd do if you had manually changed the schema AND THEN decided to write the model classes and generating the migration.
>
> Thanks for taking the time to understand it and blog about it!

Yes, it took me more time than I care to admit to get my head around how it all works. The key is to think of EF Core migrations as a dumb script runner that blindly runs through the code in your migrations and throws exceptions if it finds anything out of the ordinary.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Alexander Ekzarov](https://camo.githubusercontent.com/7f6274bf795cdd1ce37c173b35f4beac5bdedfe58c2680398c5c93f2e77d94d5/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f31386364326136363633623661383164333037343236306363636338633966313f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/7f6274bf795cdd1ce37c173b35f4beac5bdedfe58c2680398c5c93f2e77d94d5/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f31386364326136363633623661383164333037343236306363636338633966313f733d393626643d6d6d26723d67) **Alexander Ekzarov** commented on **2018-08-14 18:25:40**

No working for 2.1. HasData always generate Delete/Insert stuff in migration Up/Down methods even if no data was changed while adding new migrations.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2018-08-16 14:51:32**

> No working for 2.1. HasData always generate Delete/Insert stuff in migration Up/Down methods even if no data was changed while adding new migrations.

Raise an issue on GitHub or StackOverflow. It works for me.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Alireza Haghshenas](https://camo.githubusercontent.com/1b72d2cb9af66b59817f1e74d0e2ac69d3e46aaab7073faf084e1b0a3cad15bf/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f64376539623364393863306530613564373635373138383161386530623936633f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/1b72d2cb9af66b59817f1e74d0e2ac69d3e46aaab7073faf084e1b0a3cad15bf/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f64376539623364393863306530613564373635373138383161386530623936633f733d393626643d6d6d26723d67) **Alireza Haghshenas** commented on **2018-09-21 01:25:16**

> No working for 2.1. HasData always generate Delete/Insert stuff in migration Up/Down methods even if no data was changed while adding new migrations.

Same problem here. I add a few entities, one has data, and ef decides to delete/re-insert seed data from some of the existing entities. I could not find a logic behind why it does that and which entities it decides to delete. I check the source control history of the DbContext file as well as model snapshot and I'm pretty sure the only changes is adding new entities with data and not changing on existing model/data.

The problem I have is that I tried to reproduce it using a smaller mode, but failed to do that.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![ali](https://camo.githubusercontent.com/88ea9a57cb58df13562088dd6382917b96cabbe7aa2c573be3d6e83ba2f32d1c/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f38313837633734373733616532376563363366656433643036356436623066663f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/88ea9a57cb58df13562088dd6382917b96cabbe7aa2c573be3d6e83ba2f32d1c/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f38313837633734373733616532376563363366656433643036356436623066663f733d393626643d6d6d26723d67) **ali** commented on **2018-10-01 11:32:53**

There is already an object named in EF Core 2 next change model ?

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2018-10-23 09:49:18**

> There is already an object named in EF Core 2 next change model ?

Not sure I understand you.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Alok](https://camo.githubusercontent.com/1aa727e306d2eb9fd3e5441dba6467c2e5add294804ed8aa1d8755cbe14e3268/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f62336563646133316464393236336238376264386262633731646235386463353f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/1aa727e306d2eb9fd3e5441dba6467c2e5add294804ed8aa1d8755cbe14e3268/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f62336563646133316464393236336238376264386262633731646235386463353f733d393626643d6d6d26723d67) **Alok** commented on **2018-11-12 14:47:40**

Hi I am using Microsoft Entity Framework Core 2.0.1 and using `OnModelCreating` method to insert initial data into the database while database is created, but having compile time error while using `HasData` method, showing "could not be found or showing are you missing a directive".  
 Please shade some light of it.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2018-11-12 16:02:14**

> Hi I am using Microsoft Entity Framework Core 2.0.1 and using `OnModelCreating` method to insert initial data into the database while database is created, but having compile time error while using `HasData` method, showing "could not be found or showing are you missing a directive".  
>  Please shade some light of it.

Move to 2.1 and use data seeding. It will be less work.

[![bistok](https://avatars.githubusercontent.com/u/7431254?v=4)](https://github.com/bistok)

### [bistok](https://github.com/bistok)

19 November 2022

> [![Alireza Haghshenas](https://camo.githubusercontent.com/8fc0b66c47f71fa7ee3bdb7d3477a08a4d7ae9acc80ff238d872aabd4bf23af1/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f64376539623364393863306530613564373635373138383161386530623936633f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8fc0b66c47f71fa7ee3bdb7d3477a08a4d7ae9acc80ff238d872aabd4bf23af1/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f64376539623364393863306530613564373635373138383161386530623936633f733d393626643d6d6d26723d67) **Alireza Haghshenas** commented on **2018-09-21 01:25:16**
> > No working for 2.1. HasData always generate Delete/Insert stuff in migration Up/Down methods even if no data was changed while adding new migrations.
>
> Same problem here. I add a few entities, one has data, and ef decides to delete/re-insert seed data from some of the existing entities. I could not find a logic behind why it does that and which entities it decides to delete. I check the source control history of the DbContext file as well as model snapshot and I'm pretty sure the only changes is adding new entities with data and not changing on existing model/data.
>
> The problem I have is that I tried to reproduce it using a smaller mode, but failed to do that.

I have this problem with some types and what I found is that the Id was Auto Generated like Guid.NewGuid, the way I fixed it is that always specify a value for the key properties and that fixed the problem and now every migration do not delete and creates the objects.

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://rehansaeed.com/#website","@type":"WebSite","description":"Software Developer at Microsoft, YouTuber, Open Source Contributor and Blogger","inLanguage":"en-GB","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/","author":{"@id":"https://rehansaeed.com/#identity"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://rehansaeed.com/?search={search_term_string}"},"query-input":"required name=search_term_string"}],"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/#webpage","@type":"WebPage","dateModified":"2018-07-01T00:00:00+00:00","datePublished":"2018-07-01T00:00:00+00:00","description":"How to migrate an existing database to use Entity Framework Core 2.1 Seed Data to insert static data into your tables while using migrations.","name":"Migrating to Entity Framework Core Seed Data","url":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/","breadcrumb":{"@id":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/#breadcrumb"},"isPartOf":{"@id":"https://rehansaeed.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/"]}]},{"@id":"https://rehansaeed.com/#identity","@type":"Person","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/about/","image":{"@id":"https://rehansaeed.com/#/schema/image/1"},"sameAs":["https://github.com/RehanSaeed","https://www.linkedin.com/in/muhammad-rehan-saeed","https://stackoverflow.com/users/1212017/muhammad-rehan-saeed","https://www.youtube.com/c/MuhammadRehanSaeed","https://www.twitch.tv/rehansaeeduk"]},{"@id":"https://rehansaeed.com/#publisher","@type":"Organization","logo":"https://rehansaeed.com/images/schema/Publisher-600x60.png","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com"},{"@id":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/#article","@type":"Article","dateModified":"2018-07-01T00:00:00+00:00","datePublished":"2018-07-01T00:00:00+00:00","description":"How to migrate an existing database to use Entity Framework Core 2.1 Seed Data to insert static data into your tables while using migrations.","headline":"Migrating to Entity Framework Core Seed Data","inLanguage":"en-GB","url":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/","author":{"@id":"https://rehansaeed.com/#identity"},"image":[{"@type":"ImageObject","url":"https://rehansaeed.com/images/hero/Microsoft-.NET-1366x768.png","caption":"Microsoft .NET","width":"1366","height":"768"}],"isPartOf":{"@id":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/#webpage"},"keywords":["Entity Framework Core","Migrations","Seed Data"],"mainEntityOfPage":{"@id":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/#webpage"},"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Blog","item":"https://rehansaeed.com/","position":1},{"@type":"ListItem","name":"Migrating to Entity Framework Core Seed Data","item":"https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/","position":2}]},{"@id":"https://rehansaeed.com/#/schema/image/1","@type":"ImageObject","contentUrl":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png","inLanguage":"en-GB","url":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png"}]}
```

## Sitemap

See the full [sitemap](https://rehansaeed.com/sitemap.md) for all pages.
