Click here to Skip to main content
15,849,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a hell of figuring the problem with code. I have .net project with Entity Framework as DAL. I am using MySQL Server 8.0. Everything is working fine except when I trying to seed data. I understand the error but I have tried everything to resolve it but to no avail. The error:

An error occurred while updating the entries. See the inner exception for details.


inner exception:

MySql.Data.MySqlClient.MySqlException: Cannot add or update a child row: a foreign key constraint fails ("dbs"."branches", CONSTRAINT "FK_Branches_CompanyProfiles_CompanyProfileId" FOREIGN KEY ("CompanyProfileId") REFERENCES "companyprofiles" ("Id") ON DELETE CASCADE ON UPDATE CA)



I have my domain model and seed data.


Domain model

public class CompanyProfile
    {
        public CompanyProfile()
        {
            Branches = new HashSet<Branch>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public string CompanyCity { get; set; }
        public string CompanyAddress { get; set; }
        public string CompanyAddressTwo { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }

        //NAVIGATION
        public virtual ICollection<CompanyBranch> Branches { get; }

    }


public class Branch
    {
       
        public int Id { get; set; }
        public string BranchCity { get; set; }
        public string BranchName { get; set; }
        public virtual int CompanyProfileId { get; set; }
        public decimal BranchTarget { get; set; }

        //nagivation
        public virtual CompanyProfile CompanyProfile { get; set; }
    }


Fluent API for relationship:

class CompanyProfileConfigurations : EntityTypeConfiguration<CompanyProfile>
   {
       public CompanyProfileConfigurations()
       {
           HasMany(t => t.Branches)
           .WithRequired(x => x.CompanyProfile)
           .HasForeignKey(z => z.CompanyProfileId);
       }
   }



Seed:

context.CompanyProfiles.AddOrUpdate(
               c => c.Name,
               new CompanyProfile { Name = "New Company Profile" }
               );

           context.Branches.AddOrUpdate(
                b => b.BranchName,
                new Branch { BranchName = "Main Office", CompanyProfileId = 1, BranchCity = "", BranchTarget = 0m }
                );





Your assistance is greatly appreciated

What I have tried:

I have tried google but didn't find solution
Posted
Updated 7-Nov-19 11:53am
v2
Comments
St0rmi 7-Nov-19 7:30am    
Any chance you can check/log the SQL that's being executed on the server?
noblepaulaziz 7-Nov-19 13:48pm    
No, but the update-database commands works and its able to create the tables. It only throws up exception when the Package Manager tries to run the Seed command.

When I seed the tables one after the other, there is no exception; only happens when the seed data to the two table at the same time.
#realJSOP 8-Nov-19 10:00am    
Well, the solution is simple - stop doing that.
Gerry Schmitz 7-Nov-19 12:52pm    
It doesn't like your secondary index logic / data. Take stuff out till it works, then put it back in.
noblepaulaziz 7-Nov-19 13:49pm    
Please what do mean?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900