简体   繁体   中英

How to insert data to a relationship table in EF Core

I have two tables here (Teacher and User). Table User has a relationship to table Teacher. These are the images of Table Teacher , Table User .


I need to make a manage teacher form. So, every time I insert data to table Teacher, it must do insert the data into table User too. By my experiences, I can insert data into the table Teacher, but I can't insert the data to table User.

This is the error: An error occurred while updating the entries. See the inner exception for details” [duplicate]

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_User_Student". The conflict occurred in database "LKSN2017", table "dbo.Student", column 'StudentId'. The statement has been terminated.

I use Entity Framework Core Linq for this. This is the example of the syntax:

db.Teachers.Add(insert);

This is my code to insert the data into table Teacher and User. The variables of gender and passwordGenerate are Strings. It doesn't have a problem, so don't care about the variables.

 Teacher insert = new Teacher() { TeacherId = textboxTeacherID.Text.ToString(), Name = textboxTeacherName.Text.ToString(), Address = textboxAddress.Text.ToString(), Gender = gender, DateofBirth = Convert.ToDateTime(datepickerDateOfBirth.Text.ToString()), PhoneNumber = textboxPhoneNumber.Text.ToString() }; db.Teachers.Add(insert); User insertMore = new User() { username = textboxTeacherID.Text.ToString(), password = passwordGenerate }; db.Users.Add(insertMore); db.SaveChanges();

Ok, actually i have one more form named manage student form. It has the same task with manage teacher. Every time i insert data into table Student, it need to insert the data to table User too.

This is the Images for table Student and Table User: Table Student , Table User

So, if i can finish the manage teacher form, i can finish the manage student form too

Please help me by using the Entity Framework Core Linq. If u don't know about how to fix it by using Entity Framework Core Linq, u guys can give the MySql ways or name of technique to fix this. So, i can search it in browser. Thank you so much!

I think you need to set the relationship for Teacher and User is 1-1 example in EF

    public class Teacher
    {
        [ForeignKey("User")]
        public int UserId { get; set; }

        .............
        public virtual User User { get; set; }
    }

    public class User
    {
        [Key]
        public int Id { get; set; }

        ..............

        public virtual Teacher Teacher { get; set; }
    }

and example for insert new teacher like

var newTeacher = new Teacher()
{
    // other property
    User = new User()
    {
        //other property
    }
}
db.Teachers.Add(newTeacher);

don't forget to put the primary key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM