Skip to content

Document deployment and seeding patterns #736

Closed

Description

@Silthus

Updated to reflect the core issue we are tracking here:

TL;DR: It is very hard to come up with really good guidance on how create or update application state on deployment. Note that this is commonly related to database schema and seed data, but it is not limited to that.

Many customers end up adding logic at application startup because that runs after deployment, but this is very problematic, especially if there is more than one instance of the application using the same database.

We think this ideally should happen as a post-deployment step, not startup.

We would like to do some brainstorming with a wider group with the goal of identifying patterns that we can recommend today and/or functionality that we can build into the publish components to make this work.

Original issue as reported:

How can the key of an entity be specified? I have an entity that should be seeded based on its name and the ID (Guid) is generated automatically. Something like this:

modelBuilder.Entity<Blog>().HasData(blog => blog.Name, new Blog {Name = "New Blog", Url = "http://sample.com"});

And for composite keys:

modelBuilder.Entity<Blog>().HasData(blog => new {blog.Name, blog.Url}, new Blog {Name = "New Blog", Url = "http://sample.com"});

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Activity

  1. martinpetrovaj commented on Jun 2, 2018

    @martinpetrovaj

    Similar problem here. I was trying to seed the DB with a tree-like list of shop categories without knowing their IDs prior to seed (I'm relying on the DB to generate the ID identity key).

    builder.Entity<Category>().HasData
    			(
    				new Category()
    				{
    					Title = "Living room",
    					Url = "living-room",
    					OrderNo = 1,
    					Hidden = false,
    					ChildCategories = new List<Category>()
    					{
    						new Category() { Title = "Carpets", Url = "carpets", OrderNo = 2, Hidden = false },
    						new Category() { Title = "Flower pots", Url = "flower-pots", OrderNo = 3, Hidden = false },
    					}
    				},
    				new Category()
    				{
    					Title = "Kitchen",
    					Url = "kuchyne",
    					OrderNo = 4,
    					Hidden = false,
    					ChildCategories = new List<Category>()
    					{
    						new Category() { Title = "Dishes", Url = "dishes", OrderNo = 5, Hidden = false },
    						new Category() { Title = "Kitchen desks", Url = "kitchen-desks", OrderNo = 6, Hidden = false }
    					}
    				}
    			);

    Seeding the EF Core the old way (in Program.cs or similar place using context.Categories.Add) works fine using the same code. But I hoped that we would finally be able to avoid such ugliness with HasData.

  2. AndriySvyryd commented on Jun 2, 2018

    @AndriySvyryd
    Member

    @Silthus @martinpetrovaj The key values need to be specified explicitly as we need to make sure that they stay the same across migrations and servers.

    modelBuilder.Entity<Blog>().HasData(
        new {Name = "New Blog", Url = "http://sample.com", ID = new Guid("{8b4e4608-35da-4f7e-8f75-52d32bade14a}")});
    
  3. cephaspad commented on Jun 4, 2018

    @cephaspad

    How can I seed IdentityUser data, esp. password without using UserManager.CreateAsync

  4. AndriySvyryd commented on Jun 4, 2018

    @AndriySvyryd
    Member

    @cephaspad I think UserManager.CreateAsync normalizes the provided data. So you could call UserManager.CreateAsync on a test database, then examine the raw data inserted and use the same values in HasData

  5. ajcvickers commented on Jun 4, 2018

    @ajcvickers
    Contributor

    @AndriySvyryd @divega @HaoK Does this go against guidance from Identity where I have heard that manipulating entities not through the manager is...er...frowned upon?

  6. HaoK commented on Jun 4, 2018

    @HaoK
    Member

    Yes its definitely frowned upon as the user might have fields that aren't initialized properly, like allowLockoutForNewUsers etc etc.

  7. AlissonRS commented on Jun 10, 2018

    @AlissonRS

    There are situations we don't actually care if the key values change. I think using the same key values, different ones or allowing the database to generate them should be up to the developer to decide.

  8. AndriySvyryd commented on Jun 11, 2018

    @AndriySvyryd
    Member

    @AlissonRS The keys are required to be able to detect changes made to the seed data when a migration is generated.

  9. AlissonRS commented on Jun 11, 2018

    @AlissonRS

    @AndriySvyryd I understand your point, usually we would use some other properties (e.g the user name) as a key value in order to check if that entity already exists in the database, and change the other properties based on the anonymous object, but we wouldn't be able to change the user name (changing it would make EF assume we are seeding a new user).

    Specifying the keys makes it possible to change all properties (including the user name). But I still think it should be up to the developer to decide what key values to use (whether the actual keys or other properties like user name).

    I had to use workarounds in order to use the UserManager class for seeding data.

  10. ajcvickers commented on Jun 11, 2018

    @ajcvickers
    Contributor

    @divega @AndriySvyryd I think we need to consider guidance around seeding. Specifically, when new-style model-based seeding should be used, and when it makes more sense to continue to use more traditional seeding approaches because:

    • This is a testing scenario where the database is known to be empty and using a traditional approach allows full use of navigation properties, generated values, etc. and decouples from the relational model.
    • The entities are designed to be used through some set of APIs (e.g. Identity) such that a direct coupling of the model to the seeding would go against recommendations and be potentially error-prone and/or fragile.

    /cc @HaoK

  11. changed the title [-]How to specify entity identifier for seeding?[/-] [+]How to see the ASP.NET Identity model[/+] on Jun 11, 2018
  12. changed the title [-]How to see the ASP.NET Identity model[/-] [+]How to see the ASP.NET Identity model, and related seeding questions.[/+] on Jun 11, 2018
  13. changed the title [-]How to see the ASP.NET Identity model, and related seeding questions.[/-] [+]How to seed the ASP.NET Identity model, and related seeding questions.[/+] on Jun 11, 2018
  14. Orgbrat commented on Jun 11, 2018

    @Orgbrat

    As Recommended by the Microsoft EntityFramework.Docs GitHub issues reply, I used the process stated on http://www.binaryintellect.net/articles/5e180dfa-4438-45d8-ac78-c7cc11735791.aspx and it all worked just fine. Thanks for the assistance.

    Orgbrat

  15. added a commit that references this issue on Jun 11, 2018
    a7579bd
  16. 52 remaining items

  17. changed the title [-]Document seeding patterns[/-] [+]Document deployment and seeding patterns[/+] on Oct 1, 2020
  18. removed their assignment
    on Oct 12, 2020
  19. modified the milestones: 5.0.0, 6.0.0 on Dec 16, 2020
  20. modified the milestones: 6.0.0, 7.0.0 on Nov 4, 2021
  21. removed their assignment
    on Aug 31, 2024
  22. added a commit that references this issue on Aug 11, 2026
    bb42937
  23. modified the milestones: 7.0.0, 11.0.0 on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions