I am using the EF Core v5.0.2 & sqlserver database.
I set the Foreign key in two tables.
Primary key
Student.cs
[Key] public int Id { get; set; } public string Name { get; set; }
Foreign key
StudentAddress.cs
[Key] public int Id { get; set; } public int StudentId { get; set; } [ForeignKey("StudentId")] public virtual Student Student { get; set; }
second Foreign key
StudentDetail.cs
[Key] public int Id { get; set; } public int StudentId { get; set; } [ForeignKey("StudentId")] public virtual Student Student { get; set; }
The Context File
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; } }
Error :
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ Student Address_Student_StudentId". The conflict occurred in database "StudData", table "dbo.Student", column 'Id'. The statement has been terminated.
https://stackoverflow.com/questions/65949087/i-am-face-the-foreign-key-error-that-is-one-to-many-relations January 29, 2021 at 01:07PM
没有评论:
发表评论