EF Core seeding mechanism isn't working with relationships

c# entity-framework entity-framework-core

Question

I'm trying to simply seed two tables with EF core 3 but it's not working. It's such a small operation but it seems like EF is not able to do it.

Object model

public class Object
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        [ForeignKey("Building")]
        public int BuildingId { get; set; }

        public Building Building { get; set; }
    }

Building model

public class Building
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        public virtual List<Object> Objects { get; set; }
    }

Context class

public class AppDatabaseContext : DbContext
    {
        private static readonly DbContextOptions options = new DbContextOptionsBuilder<AppDatabaseContext>().UseInMemoryDatabase("INMEM-DB").EnableSensitiveDataLogging().Options;

        public AppDatabaseContext() : base(options)
        {

        }

        public DbSet<Building> Buildings { get; set; }

        public DbSet<Object> Objects { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Object>().HasOne(i => i.Building).WithMany(b => b.Objects);
            modelBuilder.Entity<Building>().HasMany(b => b.Objects).WithOne(i => i.Building);*/


            Building testBuilding = new Building()
            {
                Id = 1,
                Name = "Test Building"
            };

            modelBuilder.Entity<Building>(b =>
            {
                b.HasData(testBuilding);
            });


            modelBuilder.Entity<Object>(o =>
            {
                o.HasData(new Object()
                {
                    Id = 1,
                    Name = "Test Object",
                    Building = testBuilding,
                });
            });
        }
    }

Error message

System.InvalidOperationException: 'The seed entity for entity type 'Object' with the key value 'Id:1' cannot be added because it has the navigation 'Building' set. To seed relationships you need to add the related entity seed to 'Building' and specify the foreign key values {'BuildingId'}.'

Why isn't it working? Please help me. I'm about to trash this whole crap and rewrite it in Java (which would be much faster).

1
0
2/3/2020 2:30:23 PM

Accepted Answer

Just as the error says: "To seed relationships you need to add the related entity seed to 'Building' and specify the foreign key values"

In EF you have both "Foreign Key Properties" and "Navigation Properties". For most operations you can use either one, but EF Core Data Seeding requires you set the foreign key value directly.

   modelBuilder.Entity<Object>(o =>
    {
        o.HasData(new Object()
        {
            Id = 1,
            Name = "Test Object",
            //Building = testBuilding,
            BuildingId = 1 
        });
    });
1
2/3/2020 2:40:47 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow