0

The error code given was quite vague so I'm having a hard time tracing the error. I'm just assuming that it has something to do with how I related my table?

The error code is this:

{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.LGMembers_dbo.Lifegroups_LifegroupID\". 
The conflict occurred in database \"aspnet-COGSimple-20140204091131\", table \"dbo.Lifegroups\", column 'LifegroupID'.\r\nThe statement has been terminated."}

From this post INSERT statement conflicted with the FOREIGN KEY constraint, It said that:

The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table

However, my tables are referenced in such a way that every fk is linked to a Primary Key.

The tables affected:

public class Lifegroup
{
    [Key]
    public int LifegroupID { get; set; }
    [Display(Name = "Lifegroup Name")]
    public string LGName { get; set; }
    [Display(Name = "Lifegroup Type")]
    public string LGType { get; set; }
    public DateTime DateCreated { get; set; }

    public int LGLeaderID { get; set; }

    [ForeignKey("LGLeaderID")]
    public virtual LGLeader LGLeader { get; set; }
}

public class LGLeader
{
    [Key]
    public int LGLeaderID { get; set; }

    public string UserID { get; set; }

    [ForeignKey("UserID")]
    public virtual User User { get; set; }
}

public class LGMember
{
    [Key]
    public int LGMemberID { get; set; }

    public string UserID { get; set; }
    public int LifegroupID { get; set; }

    [ForeignKey("UserID")]
    public virtual User User { get; set; }
    [ForeignKey("LifegroupID")]
    public virtual Lifegroup Lifegroup { get; set; }
}

Thank you very much!

Edit:

The error comes from a db.SaveChanges() when trying to insert data to LGMember.

Community
  • 1
  • 1
Dominic
  • 928
  • 3
  • 9
  • 19
  • Can you try to remove the foreign key data annotation before any FK attribute. Or change the ForeignKey Data annotation by [Required]. Please let me know the result? – Tassisto Mar 24 '14 at 10:48
  • Did you solve this issue? Can you tell us how. – Sahar Ch. May 05 '14 at 09:18
  • Hi, @Elsa. I did, but it involved restructuring most of my Models. In Layman's term this error is basically telling you that you should first Populate the tables that you're trying to reference to. My error is saying that I should first have data in my 'Lifegroup' Table before I can reference it to my 'LGMember'. I hope this makes sense, still haven't improved much after all this time. Haha. – Dominic May 06 '14 at 08:26
  • yeah, I figured out later that it was all about actually having the right data (the one you're calling). They obviously should exist before we can manipulate them :p Good to know haha! – Sahar Ch. May 06 '14 at 08:36

0 Answers0