Nested insert using LINQ

Consider database structure we have created in previous post. Both tables share a relationship.
Now if one wants to insert records in both the tables, 2 seperate queries are required to update each table.
LINQ provides a mechanism where tables are interdependent it manages the insertion of records. i.e. using LINQ one need to fire the insert query only once and that on parent table.

Database structure is;
Here is a code to illustrate that;

string employeename = txtEmployeeName.Text;
string deptName = txtDeptName.Text;

Employee emp = new Employee();
emp.EmployeeName = employeename;

Department dept = new Department();
dept.DeptName = deptName;


This will insert records in both the tables at one insert request and it will 
handle the responsibility of updating references.

No comments:

Post a Comment