dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.77k stars 3.18k forks source link

How to Seed Many to Many entity #23363

Closed tinquen12 closed 2 years ago

tinquen12 commented 3 years ago

Hello,

I'm trying to seed data in efcore5 using auto many to many relation between Group and PolicyTarget (see below the model)

Here is my models👍:

/// <summary>
    /// Represents a Group of users
    /// </summary>
    public class Group : PolicyTarget
    {
        public Group()
        {
            this.Children = new List<PolicyTarget>();
        }

        /// <summary>
        /// The name of the group
        /// </summary>
        [Required]
        [StringLength(100)]
        public string Name { get; set; }

        /// <summary>
        /// The Description of the group
        /// </summary>
        [StringLength(500)]
        public string Description { get; set; }

        /// <summary>
        /// Comma separated string of AD group name
        /// </summary>
        [NotMapped]
        public List<string> LinkedAdGroups { get; set; }

        /// <summary>
        /// The name of the Identity role to map to the group
        /// </summary>
        //public IEnumerable<string> IdentityRole { get; set; }

        /// <summary>
        /// Children
        /// </summary>
        public virtual ICollection<PolicyTarget> Children { get; set; }

    }

And here is my seeding method :

modelBuilder.Entity<Group>()
                .HasData(
                    new Group { Id = 1, Name = "SuperAdmin", Description = "Super Admin" },
                    new Group { Id = 2, Name = "Admin", Description = "Admin", Children = new List<PolicyTarget>()
                        {
                            new Group { Id = 3, Name = "Admin1", Description = "Admin 1" },
                            new Group { Id = 5, Name = "Admin2", Description = "Admin 2" },
                            new Group { Id = 6, Name = "Admin3", Description = "Admin 3" },
                        }
                    }
                );

I receive the following error :

System.InvalidOperationException: The seed entity for entity type 'Group' cannot be added because it has the navigation 'Children' set. To seed relationships,  add the entity seed to 'GroupPolicyTarget (Dictionary<string, object>)' and specify the foreign key values {'MemberOfId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.

What am I missing ?

Thank you very much 😄

ajcvickers commented 3 years ago

@tinquen12 See the example in https://github.com/dotnet/EntityFramework.Docs/issues/2879. We will also be adding this to the docs.

tinquen12 commented 3 years ago

Hello thank you very much for the answer.

Is there any chance we can seed m2m data in a specific file containing only the HasData ?

ajcvickers commented 3 years ago

@tinquen12 You should be able to factor the code into as many files as you want.