model view controller - Insert data into multiple table in entity framework -
i have 2 tables below. using db first approch
public class team { public long id { get; set; } public string name { get; set; } public string description { get; set; } } public class employee { public long id { get; set; } public long teamid { get; set; } // foreign key reference fom team table public string name { get; set; } public long referedby { get; set; } // foreign key reference fom employee table }
now have insert data both table once. example need insert data below.
before introducing referred column using below code insert.
team team = new team(); team.name = "team1"; team.description = "some description"; employee e1 = new employee(); e1.name = "jon"; employee e2 = new employee(); e2.name = "harish"; team.employee.add(e1); team.employee.add(e2); dbentity db = new dbentity(); db.set<team>().add(team); db.savechanges();
after introducing referred column how can insert these reference database.
public long teamid { get; set; } // foreign key reference fom team table
you need one-to-one relationship see post:
before introducing referred column using below code insert.
you need here self referencing entity:
Comments
Post a Comment