0
votes

I have used foreign keys many times before and set up these models just the same however I'm getting this error, the error also occurs when writing usertableID:

A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_dbo.Outreaches_dbo.OutreachNames_OutreachNamesID ]

Can anyone explain?

Code causing error:

            foreach (var item in records)
            {
                List<string> foundEmails = EmailScraper.Main(item.domain);
                string[] emails = foundEmails.ToArray();
                var outreach = new Outreach {
                    domain = item.domain,
                    email1 = foundEmails.ElementAtOrDefault(0),
                    email2 = foundEmails.ElementAtOrDefault(1),
                    email3 = foundEmails.ElementAtOrDefault(2),
                    email4 = foundEmails.ElementAtOrDefault(3),
                    email5 = foundEmails.ElementAtOrDefault(4),
                    email6 = foundEmails.ElementAtOrDefault(5),
                    UserTableID = UserTableID,
                    OutreachNamesID = listNumber

                };
                db.OutreachLists.Add(outreach);
                db.SaveChanges();

            }
            var outreachlist = new OutreachNames
            {
                ID = listNumber,
                listName = model.listName,
                listCount = count,
                listSent = 0,
                unread = 0,
                replyRate = 0,
                UserTableID = UserTableID,

            };
            db.OutreachNames.Add(outreachlist);
            db.SaveChanges();

Model Outreach:

namespace Linkofy.Models
{
public class Outreach
{
    public int ID { get; set; }

    public int? OutreachNamesID { get; set; }
    public virtual OutreachNames OutreachNames { get; set; }

    public string name { get; set; }

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

    public string email1 { get; set; }
    public string email2 { get; set; }
    public string email3 { get; set; }
    public string email4 { get; set; }
    public string email5 { get; set; }
    public string email6 { get; set; }
    public string email7 { get; set; }
    public string email8 { get; set; }

    public int? UserTableID { get; set; }
    public virtual UserTable UserTable { get; set; }
}
}

Model OutreachNames:

namespace Linkofy.Models
{
public class OutreachNames
{
    public int ID { get; set; }

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

    public int listCount { get; set; }
    public int listSent { get; set; }
    public int unread { get; set; }
    public int replyRate { get; set; }

    public virtual ICollection<Outreach> OutreachLists { get; set; }

    public int? UserTableID { get; set; }
    public virtual UserTable UserTable { get; set; }
}
}
1

1 Answers

2
votes

When saving your Outreach you are setting the FK OutreachNamesID to an ID of a record which doesn't exist yet. You need to create this record first or use Entity Framework to create OutreachNames as a child entity. Both entities need to be persisted to the db in one transaction.

You can create the child entity inside of the parent and persist them in one go like this:

var outreach = new Outreach
{
    OutreachNames = new OutreachNames
    {
        ...
    }
}

db.OutreachLists.Add(outreach);
db.SaveChanges();