获取错误无法插入外键值,因为不存在相应的主键值。 [外键约束名称= ...] [英] Get error a foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = ...]

查看:219
本文介绍了获取错误无法插入外键值,因为不存在相应的主键值。 [外键约束名称= ...]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在EF中创建外键但我一直收到错误消息

无法插入外键值,因为不存在相应的主键值。



学生已经引用了AddressDetail,我正在尝试创建另一个参考Employee to AddressDetail ..



< pre lang =c#> public abstract class BaseEntity
{
[Key]
public int Id {获取; set ; }
public DateTime? AddedDate { get ; set ; }
public string AddedBy { get ; set ; }
public DateTime? ModifiedDate { get ; set ; }
public string ModifiedBy { get ; set ; }
}

public class 学生:BaseEntity
{
public string AppNo { get < /跨度>; set ; }
public string 名称{ get ; set ; }
public virtual ICollection< addressdetail> AddressDetails { get ; set ; }
}

public class 员工:BaseEntity
{
public string 名称{ get < /跨度>; set ; }
public virtual ICollection< addressdetail> AddressDetails { get ; set ; }
}

public class AddressDetail:BaseEntity
{
public int RefId { get < /跨度>; set ; }
public string 地址{ get ; set ; }
public virtual 学生{ get < /跨度>; set ; }
public virtual 员工雇员{ get < /跨度>; set ; }
}

受保护 覆盖 void OnModelCreating(DbModelBuilder modelBuilder)
{
base .OnModelCreating(modelBuilder);

modelBuilder.Entity< addressdetail>()
.HasRequired(i = > i.Student)
。 WithMany(a = > a.AddressDetails)
.HasForeignKey(f = > f .RefId)
.WillCascadeOnDelete( false );

modelBuilder.Entity< addressdetail>()
.HasRequired(i = > i.Employe)
。 WithMany(a = > a.AddressDetails)
.HasForeignKey(f = > f .RefId)
.WillCascadeOnDelete( false );
}





我的尝试:



使用(var ctx = new InstituteContext())

{

学生stu =新学生

{

AppNo =A001,

名称=Mohan,

地址详细信息=新列表< addressdetail>

{

新地址详情

{

RefId = Id,//来自BaseEntity的学生主要钥匙

地址=瓦拉纳西

}

};

ctx.Add(stu);

ctx.Save(); < br $>
}

}



我做错了什么?

请帮帮我gyes



谢谢

Manmohan

解决方案

您正试图插入一个没有Emp的AddressDetail实体loyee实体关联。这是根据你的映射代码。

在插入或修复映射之前也要创建一个雇员实体。


这是一个数据库设计问题。

Quote:

我正在尝试在EF中创建一个外键但我一直收到错误消息

无法插入外键值,因为不存在相应的主键值。



学生已经引用AddressDetail而我正在尝试创建另一个参考Employee to AddressDetail ..



您的数据库是在 AddressDetail外键值必须与<$匹配的约束下创建的c $ c>学生主键值。

当您尝试使用 employees主键值插入AddressDetail ,您违反了约束,因为 employees主键值与任何学生主键值不匹配。 / BLOCKQUOTE>

I'm trying to create a foreign key in EF but I keep getting the error message
"A foreign key value cannot be inserted because a corresponding primary key value does not exist."

Student is referencing AddressDetail already and I'm trying to create another reference Employee to AddressDetail..

public abstract class BaseEntity
    {        
        [Key]
        public int Id { get; set; }
        public DateTime? AddedDate { get; set; }
        public string AddedBy { get; set; }
        public DateTime? ModifiedDate { get; set; }
        public string ModifiedBy { get; set; }
    }

public class Student : BaseEntity
    {
        public string AppNo { get; set; }
        public string Name { get; set; }
        public virtual ICollection<addressdetail> AddressDetails { get; set; }
    }

public class Employe : BaseEntity
    {       
        public string Name { get; set; }
        public virtual ICollection<addressdetail> AddressDetails { get; set; }
    }

public class AddressDetail : BaseEntity
    {
        public int RefId { get; set; }
        public string Address { get; set; }
        public virtual Student Students { get; set; }
        public virtual Employe Employes { get; set; }
    }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<addressdetail>()
                .HasRequired(i => i.Student)
                .WithMany(a => a.AddressDetails)
                .HasForeignKey(f => f.RefId)
                .WillCascadeOnDelete(false);

           modelBuilder.Entity<addressdetail>()
                .HasRequired(i => i.Employe)
                .WithMany(a => a.AddressDetails)
                .HasForeignKey(f => f.RefId)
                .WillCascadeOnDelete(false);
    }



What I have tried:

using (var ctx = new InstituteContext())
{
Student stu=new Student
{
AppNo="A001",
Name="Mohan",
AddressDetails = new List<addressdetail>
{
new AddressDetail
{
RefId= Id, // Student Primary key Geting from BaseEntity
Address = "Varanasi"
}
};
ctx.Add(stu);
ctx.Save();
}
}

What i am doing wrong?
Please help me gyes

Thanks
Manmohan

解决方案

You are trying to insert an AddressDetail entity with no Employee entity associated. That's according to your mapping code.
Either create an employee entity as well before insert or fix the mapping.


This is a database design problem.

Quote:

I'm trying to create a foreign key in EF but I keep getting the error message
"A foreign key value cannot be inserted because a corresponding primary key value does not exist."

Student is referencing AddressDetail already and I'm trying to create another reference Employee to AddressDetail..


Your database was created with the constraint that the AddressDetail foreign key value must match a student primary key value.
When you try to insert an AddressDetail with an employe primary key value, you violate the constraint because the employe primary key value don't match any student primary key value.


这篇关于获取错误无法插入外键值,因为不存在相应的主键值。 [外键约束名称= ...]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆