none
ForeignKeyReferenceAlreadyHasValueException RRS feed

  • Question

  • I am trying to empty three tables in my mdf file, then fill all of them with new records with my  code. The Delete queries work fine to empty the tables of all records. When I try to re populate the tables from a list<> I can populate the Clients.dbo no problem, but a Child table of Clients is Horse.dbo that has a foreign key constraint that a client an not be deleted if it has horse records in the horse.dbo, when I try to let my code below add records I get this error.

    "ForeignKeyReferenceAlreadyHasValueException" Even though the horse.dbo is empty.

    Delete statements are simple

    void IService1.DeleteClients()
      {
        using (DataClassesFB8mdfDataContext db = new DataClassesFB8mdfDataContext())
        {
          int rec = db.Clients.Count();
          db.ExecuteCommand("DELETE Client");
          int reAfter = db.Clients.Count();
        }
      }
       void IService1.DeleteHorses()
       {
         using (DataClassesFB8mdfDataContext db = new DataClassesFB8mdfDataContext())
         {
           int rec = db.Horses.Count();
           db.ExecuteCommand("DELETE Horse");
           int recAfter = db.Horses.Count();
           
         }
       }
        void IService1.DeleteHrsPics()
        {
          using (DataClassesFB8mdfDataContext db = new DataClassesFB8mdfDataContext())
          {
            int rec = db.HorsePics.Count();
            db.ExecuteCommand("DELETE HorsePics");
            int recAfter = db.HorsePics.Count();
          }
         
        }
    
    

    I have tried inserting the Clients first then the horses and the horses first then the clients, no change in the error.

    How can I change the way I am inserting records or how can I set the horse table to allow the insert?

    Here is code to insert records

    ListAllHorses();
          proxy = new Service1Client();
          Horse[] horseList = horses.ToArray();
          foreach (Horse h in horseList)
          {
            Horse hr = new ServiceReference1.Horse();
            hr.Active = h.Active;
            hr.AngleLF = h.AngleLF;
            hr.AngleLH = h.AngleLH;
            hr.AngleList = h.AngleList;
            hr.AngleRF = h.AngleRF;
            hr.AngleRH = h.AngleRH;
            hr.Code = h.Code;
            hr.DeleteHrs = h.DeleteHrs;
            hr.DueDate = hr.DueDate;
            hr.Front = h.Front;
            hr.Hind = h.Hind;
            hr.HrsId = h.HrsId;
            hr.HorseName = h.HorseName;
            hr.LastCost = h.LastCost;
            hr.LastDate = h.LastDate;
            hr.LengthLF = h.LengthLF;
            hr.LengthLH = h.LengthLH;
            hr.LengthList = h.LengthList;
            hr.LengthRF = h.LengthRF;
            hr.LengthRH = h.LengthRH;
            hr.MainWork = h.MainWork;
            hr.NeedNewHd = h.NeedNewHd;
            hr.NeedsNewFr = h.NeedsNewFr;
            hr.NickName = h.NickName;
            //hr.Picture = h.Picture;
            hr.SizeLF = h.SizeLF;
            hr.SizeLH = h.SizeLH;
            hr.SizeRF = h.SizeRF;
            hr.SizeRH = h.SizeRH;
            hr.StableCode = h.StableCode;
            hr.StartAngleLF = h.StartAngleLF;
            hr.StartAngleLH = h.StartAngleLH;
            hr.StartAngleRF = h.StartAngleRF;
            hr.StartAngleRH = h.StartAngleRH;
            hr.StartLengthLF = h.StartLengthLF;
            hr.StartLengthLH = h.StartLengthLH;
            hr.StartLengthRF = h.StartLengthRF;
            hr.StartLengthRH = h.StartLengthRH;
            hr.Totals = h.Totals;
            hr.Weeks = h.Weeks;
            proxy.InsertAllHorses(hr);
          }
    
    <pre lang="x-c#">void IService1.InsertAllHorses(Horse h)
        {
        using (DataClassesFB8mdfDataContext db = new DataClassesFB8mdfDataContext())
        {
          
          int rec = db.Horses.Count();
          Horse hr = new Horse()
                  {
                   Active = h.Active,
                    AngleLF = h.AngleLF,
                    AngleLH = h.AngleLH,
                    AngleList = h.AngleList,
                    AngleRF = h.AngleRF,
                    AngleRH = h.AngleRH,
                    Code = h.Code,
                    DeleteHrs = h.DeleteHrs,
                    DueDate = h.DueDate,
                    Front = h.Front,
                    Hind = h.Hind,
                    HorseName = h.HorseName,
                    HrsId = h.HrsId,
                    LastCost = h.LastCost,
                    LastDate = h.LastDate,
                    LengthLF = h.LengthLF,
                    LengthLH = h.LengthLH,
                    LengthList = h.LengthList,
                    LengthRF = h.LengthRF,
                    LengthRH = h.LengthRH,
                    MainWork = h.MainWork,
                    NeedNewHd = h.NeedNewHd,
                    NeedsNewFr = h.NeedsNewFr,
                    NickName = h.NickName,
                    SizeLF = h.SizeLF,
                    SizeLH = h.SizeLH,
                    SizeRF = h.SizeRF,
                    SizeRH = h.SizeRH,
                    StableCode = h.StableCode,
                    StartAngleLF = h.StartAngleLF,
                    StartAngleLH = h.StartAngleLH,
                    StartAngleRF = h.StartAngleRF,
                    StartAngleRH = h.StartAngleRH,
                    StartLengthLF = h.StartLengthLF,
                    StartLengthLH = h.StartLengthLH,
                    StartLengthRF = h.StartLengthRF,
                    StartLengthRH = h.StartLengthRH,
                    Totals = h.Totals,
                    Weeks = h.Weeks
                  };
                  db.Horses.InsertOnSubmit(hr);
                   int recAfter = db.Horses.Count();
                  db.SubmitChanges();
        }
    
        }
    
    
    
    Sunday, February 27, 2011 10:25 PM

Answers

All replies