问题描述
我使用的是 EF Core v5.0.2 和一个 sql Server 数据库。
我在两个表中设置了外键。
主键:Student.cs
[Key]
public int Id { get; set; }
public string Name { get; set; }
外键:StudentAddress.cs
[Key]
public int Id { get; set; }
public int StudentId { get; set; }
[ForeignKey("StudentId")]
public virtual Student Student { get; set; }
第二个外键:StudentDetail.cs
[Key]
public int Id { get; set; }
public int StudentId { get; set; }
[ForeignKey("StudentId")]
public virtual Student Student { get; set; }
DbContext
文件:
public class Context : DbContext
{
public Context(DbContextOptions<Context> options) : base(options)
{
}
public DbSet<Student> Student { get; set; }
public DbSet<StudentAddress> StudentAddress { get; set; }
public DbSet<StudentDetail> StudentDetail { get; set; }
}
我收到此错误:
sqlException: INSERT 语句与 FOREIGN KEY 约束“FK_ Student Address_Student_StudentId”冲突。冲突发生在数据库“StudData”、表“dbo.Student”、“Id”列中。声明已终止。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)