RehanSaeed / rehansaeed.github.io

Muhammad Rehan Saeed's Blog
https://rehansaeed.com
30 stars 6 forks source link

[Comment] Migrating to Entity Framework Core Seed Data #88

Open RehanSaeed opened 4 years ago

RehanSaeed commented 4 years ago

https://rehansaeed.com/migrating-to-entity-framework-core-seed-data/

RehanSaeed commented 4 years ago

Martín Coll 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 commented 4 years ago

Muhammad Rehan Saeed Muhammad Rehan Saeed 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 commented 4 years ago

Alexander Ekzarov 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 commented 4 years ago

Muhammad Rehan Saeed Muhammad Rehan Saeed 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 commented 4 years ago

Alireza Haghshenas 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 commented 4 years ago

ali ali commented on 2018-10-01 11:32:53

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

RehanSaeed commented 4 years ago

Muhammad Rehan Saeed Muhammad Rehan Saeed 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 commented 4 years ago

Alok 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 commented 4 years ago

Muhammad Rehan Saeed Muhammad Rehan Saeed 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 commented 1 year ago

Alireza Haghshenas 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.