0

I'm working with Entity Framework CF and i have weird problem,

Entities in 'StoreContext.Coolers' participate in the
'Cooler_PCParts' relationship. 0 related 'Cooler_PCParts_Target' were found. 1 'Cooler_PCParts_Target' is expected.

In StoreInitializer Cooler is first and that's why shows the error with it, but when i change positions and e.g. Discs are first it shows the error with Disc_PCParts property.

my PCParts Initializer:

var pcparts = new List<PCParts>
            {
                new PCParts { PCPartsID = 1, ElectronicID= 3, Name= "Chłodzenie"},
                new PCParts { PCPartsID = 2, ElectronicID= 3, Name= "Dyski"},
                new PCParts { PCPartsID = 3, ElectronicID= 3, Name= "Karty dźwiękowe"},
                new PCParts { PCPartsID = 4, ElectronicID= 3, Name= "Karty Graficzne"},
                new PCParts { PCPartsID = 5, ElectronicID= 3, Name= "Kontrolery PCI"},
                new PCParts { PCPartsID = 6, ElectronicID= 3, Name= "Napędy optyczne"},
                new PCParts { PCPartsID = 7, ElectronicID= 3, Name= "Obudowy Komputera"},
                new PCParts { PCPartsID = 8, ElectronicID= 3, Name= "Pamięć RAM"},
                new PCParts { PCPartsID = 9, ElectronicID= 3, Name= "Płyty główne"},
                new PCParts { PCPartsID = 10, ElectronicID= 3, Name= "Procesory"},
                new PCParts { PCPartsID = 11, ElectronicID= 3, Name= "Zasilacze"},

            };

            pcparts.ForEach(o => context.PCParts.AddOrUpdate(o));
            context.SaveChanges();

Cooler Initializer:

var coolers = new List<Cooler>
            {
                new Cooler { CoolerID = 1, PCPartsID = 1, ProducerID = 21, Name = "cooler 80 mm", Warranty = "2 lata", Size = "80 mm", RotationSpeed = "2200 obr./min.", MaxNoise = "23 dB", Tension = "12 V", Dimensions = "80 x 80 x 25 mm", AirFlow = "25", Price = 24.90M }, // ok
                new Cooler { CoolerID = 2, PCPartsID = 1, ProducerID = 21, Name = "cooler 92 mm", Warranty = "2 lata", Size = "92 mm", RotationSpeed = "1800 obr./min.", MaxNoise = "24 dB", Tension = "12 V", Dimensions = "92 x 92 x 25 mm", AirFlow = "25", Price = 12.90M }, //ok
                new Cooler { CoolerID = 3, PCPartsID = 1, ProducerID = 22, Name = "Amber cooler 120 mm - Ultra Cichy", Warranty = "2 lata", Size = "120 mm", RotationSpeed = "1400 obr./min.", MaxNoise = "18 dB", Tension = "12 V", Dimensions = "120 x 120 x 25 mm", AirFlow = "44.8", LifeOfCooler = "80000 godz.", AdditionalFeatures = "łopatki wirnika w kolorze pomarańczowym", Price = 41.90M},//ok
                new Cooler { CoolerID = 4, PCPartsID = 1, ProducerID = 22, Name = "Amber cooler 80 mm - Ultra Cichy", Warranty = "2 lata", Size = "80 mm", RotationSpeed = "1800 obr./min.", MaxNoise = "20 dB", Tension = "12 V", Dimensions = "80 x 80 x 25 mm", AirFlow = "24.4", LifeOfCooler = "80000 godz.", AdditionalFeatures = "łopatki wirnika w kolorze pomarańczowym", Price = 32.90M}, //ok

                new Cooler { CoolerID = 5, PCPartsID = 1, ProducerID = 23, Name = "Dark Blue 120 mm", Warranty = "2 lata", Size = "120 mm", RotationSpeed = "1200 obr./min.", MaxNoise = "22.5 dB", Tension = "12 V", Dimensions = "120 x 120 x 25 mm", AirFlow = "49.21", Price = 25.90M, AdditionalFeatures = "wentylator z niebieskimi diodami" }, //ok
                new Cooler { CoolerID = 6, PCPartsID = 1, ProducerID = 23, Name = "AirGuard 50 mm", Warranty = "2 lata", Size = "50 mm", RotationSpeed = "4500 obr./min.", MaxNoise = "25.84 dB", Dimensions = "50 x 50 x 10 mm", LifeOfCooler = "50000 godz.", AdditionalFeatures = "złącze: 3-Pin Molex", Price = 11.90M }, //ok
                new Cooler { CoolerID = 7, PCPartsID = 1, ProducerID = 23, Name = "AirGuard 92 mm", Warranty = "2 lata", Size = "92 mm", RotationSpeed = "2000 obr./min.", MaxNoise = "25.87 dB", Dimensions = "92 x 92 x 25 mm", LifeOfCooler = "50000 godz.", AdditionalFeatures = "3-Pin Molex", Price = 12.90M }, //ok
                new Cooler { CoolerID = 8, PCPartsID = 1, ProducerID = 24, Name = "Zephyr 60 mm", Warranty = "2 lata", Size = "60 mm", RotationSpeed = "2500 obr./min.", MaxNoise = "17.9 dB", Tension = "12 V", Dimensions = "60 x 60 x 15 mm", AirFlow = "33.66", LifeOfCooler = "50000 godz.", Price = 9.55M }, //ok
                new Cooler { CoolerID = 9, PCPartsID = 1, ProducerID = 24, Name = "Zephyr 140 mm", Warranty = "2 lata", Size = "140 mm", RotationSpeed = "600 obr./min.", MaxNoise = "8.9 dB", Tension = "12 V", Dimensions = "140 x 140 x 25 mm", AirFlow = "47", LifeOfCooler = "50000 godz." , AdditionalFeatures = "Wtyczka 3 pin | W komplecie śrubki + silikonowe podkładki",Price = 19.50M }, //ok

            };

            coolers.ForEach(c => context.Coolers.AddOrUpdate(c));
            context.SaveChanges();

PCParts model:

public class PCParts
    {
        public int PCPartsID { get; set; }
        public int ElectronicID { get; set; }

        public string Name { get; set; }

        public virtual Electronic Electronic { get; set; }
        public virtual ICollection<SoundCard> SoundCards { get; set; }
        public virtual ICollection<Disc> Discs { get; set; }
        public virtual ICollection<Cooler> Coolers { get; set; }
        public virtual ICollection<Graphic> Graphics { get; set; }
        public virtual ICollection<RAM> RAMs { get; set; }
        public virtual ICollection<CPU> CPUs { get; set; }
        public virtual ICollection<ComputerCase> ComputerCases { get; set; }
        public virtual ICollection<Motherboard> Motherboards { get; set; }

    }

Cooler model:

public class Cooler
    {
        public int CoolerID { get; set; }
        public int ProducerID { get; set; }
        public int PCPartsID { get; set; }

        public string Name { get; set; }
        public string Warranty { get; set; }
        public string Size { get; set; }
        public string RotationSpeed { get; set; }
        public string MaxNoise { get; set; }
        public string Tension { get; set; }
        public string Dimensions { get; set; }
        public string AirFlow { get; set; }
        public string AdditionalFeatures { get; set; }
        public string LifeOfCooler { get; set; }
        public decimal Price { get; set; }



        public virtual Producer Producer { get; set; }
        public virtual PCParts PCParts { get; set; }
    }

And in the StoreContext i have sth like this, because my FOREIGN KEY have multiple cascades

        modelBuilder.Entity<Cooler>().HasRequired(c => c.PCParts).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<DiscType>().HasRequired(c => c.PCParts).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<SoundCard>().HasRequired(c => c.PCParts).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Graphic>().HasRequired(c => c.PCParts).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<ComputerCase>().HasRequired(c => c.PCParts).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<RAM>().HasRequired(c => c.PCParts).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Motherboard>().HasRequired(c => c.PCParts).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<CPU>().HasRequired(c => c.PCParts).WithMany().WillCascadeOnDelete(false);
  • 1
    Make sure you use the fluent API overloads with navigation properties when present. For instance, for `Cooler` it should be `.WithMany(p => p.Coolers)`. Similar for others. You can't simply omit them. The parameterless overloads are for scenarios when you don't have inverse navigation property. – Ivan Stoev Dec 14 '17 at 21:00
  • I used this: https://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths, this is wrong what i wrote? It worked :/ –  Dec 14 '17 at 21:07
  • In Cooler i can't write Coolers i don't have this property in my Model, PCPartsID is my FOREIGN KEY to table PCParts, what should i change ? –  Dec 14 '17 at 21:09
  • 1
    your specific problem is caused by the fact the PCParts property of the Cooler entity is required, yet in the given entries not set, therefore it can't be saved into the database. Note also that all the navigation properties from PCParts back to the other part types are not mapped, as @IvanStoev noted. Also, it appears the FK is not mapped to the navigation property, you should do that to make the mapping consistent. – DevilSuichiro Dec 14 '17 at 21:19

1 Answers1

1

Yup i fix that. My problem was that i had bad overloards like @Ivan Stoev said. Everyones now works good and db is updated well. Thank you for help.

My StoreContext file now looks:

modelBuilder.Entity<Laptop>().HasRequired<Electronic>(c => c.Electronic).WithMany(c => c.Laptops).HasForeignKey<int>(c => c.ElectronicID).WillCascadeOnDelete(false);
            modelBuilder.Entity<PC>().HasRequired<Electronic>(c => c.Electronic).WithMany(c => c.PCs).HasForeignKey<int>(c => c.ElectronicID).WillCascadeOnDelete(false);
            modelBuilder.Entity<PCParts>().HasRequired<Electronic>(c => c.Electronic).WithMany(c => c.PCParts).HasForeignKey<int>(c => c.ElectronicID).WillCascadeOnDelete(false);
            modelBuilder.Entity<Network>().HasRequired<Electronic>(c => c.Electronic).WithMany(c => c.Networks).HasForeignKey<int>(c => c.ElectronicID).WillCascadeOnDelete(false);

            modelBuilder.Entity<Cooler>().HasRequired<PCParts>(c => c.PCParts).WithMany(c => c.Coolers).HasForeignKey<int>(c => c.PCPartsID);
            modelBuilder.Entity<DiscType>().HasRequired<PCParts>(c => c.PCParts).WithMany(c => c.DiscTypes).HasForeignKey<int>(c => c.PCPartsID);
            modelBuilder.Entity<SoundCard>().HasRequired<PCParts>(c => c.PCParts).WithMany(c => c.SoundCards).HasForeignKey<int>(c => c.PCPartsID);