无法插入.NET外键

时间:2018-02-02 16:13:57

标签: c# .net asp.net-mvc entity-framework

之前我曾多次使用外键并且设置这些模型的方式相同然而我收到此错误,写入usertableID时也会出错:

  

无法插入外键值,因为不存在相应的主键值。 [外键约束名称= FK_dbo.Outreaches_dbo.OutreachNames_OutreachNamesID]

任何人都可以解释一下吗?

代码导致错误:

            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();

模特外展:

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 个答案:

答案 0 :(得分:2)

保存Outreach时,您将FK OutreachNamesID设置为尚未存在的记录的ID。您需要首先创建此记录,或使用Entity Framework将OutreachNames创建为子实体。两个实体都需要在一次交易中持久保存到数据库中。

您可以在父级内部创建子实体,并将其一次性保存:

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

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