获取错误无法插入外键值,因为不存在相应的主键值。 [外键约束名称= ...] [英] Get error a foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = ...]
问题描述
我正在尝试在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 theAddressDetail foreign key value
must match astudent primary key value
.
When you try to insert an AddressDetail with anemploye primary key value
, you violate the constraint because theemploye primary key value
don't match anystudent primary key value
.
这篇关于获取错误无法插入外键值,因为不存在相应的主键值。 [外键约束名称= ...]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!