DLINK gives maximum flexibility in manipulation data with objects. You must retrieve the object before you update or delete it, and you must initialize a new object before inserting it into the db.
Let's see some examples:
Insert a new employee:
MyDbDataContext db = new MyDbDataContext();
// Init a new employee object using Object Initializer
Employee emp = new Employee()
{ Id = 10, Name = "New Employee", DepartmentCode = 5 };
db.Employees.Add(emp);
db.SubmitChanges();
You can see that I must create a new object before I added it to the Employee collection. After I added it, I must call the SubmitChanges method in order to commit the operation.
Delete an employee:
MyDbDataContext db = new MyDbDataContext();
Employee emp = db.Employees.Single(x => x.Id == 10);
db.Employees.Remove(emp);
db.SubmitChanges();
In this case the code is very similar to the insert operation (except that I retrieve the employee before).
Update an existing employee:
MyDbDataContext db = new MyDbDataContext();
Employee emp = db.Employees.Single(x => x.Id == 10);
emp.DepartmentCode = 1;
db.SubmitChanges();
In this case I retrieved an existing employee and changed his department code. In this case we didn't need to get throw the employee collection, we could update his details directly.
Read more:
Introducing to DLINQ
· Introducing to DLINQ - Entities declaration - part 1
· Introducing to DLINQ – How to write the queries - part 2
· Introducing to DLINQ – How to write data manipulation in DLINQ (Update, insert and delete) - part 3
Advanced topics in DLINQ:
· DLINQ – Advanced topics – Transaction support – part 4
· DLINQ – Advanced topics – How it works in ASP.NET application – part 5
· DLINQ – Advanced topics – Debug mode – part 6