Not able to save data in teacher table in entity frame work core

asp.net asp.net-core entity-framework-core

Question

I have define this one to many relation in academy and teacher table

modelBuilder.Entity<Teacher>().HasKey(x => x.Id);
modelBuilder.Entity<Teacher>().Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Teacher>().Property(x => x.FirstName).HasMaxLength(100).IsRequired();
modelBuilder.Entity<Teacher>().Property(x => x.LastName).HasMaxLength(100).IsRequired();
modelBuilder.Entity<Teacher>().HasOne(a => a.Academy).WithMany(t => t.Teachers).HasForeignKey(a => a.AcademyId);

when try to save data in teacher table giving error of null entry in academy table but i try to save in teacher table please help to solve this

1
0
3/27/2020 10:49:21 AM

Accepted Answer

The teacher is a member of an academy, therefore you cannot save a teacher without specifying his/her academy, see

HasForeignKey(a => a.AcademyId);

and read the docs for HasForeignKey. This specifies that AcademyId is a foreign key.

0
3/27/2020 10:50:10 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow