m:n save

Posts   
 
    
Krish
User
Posts: 91
Joined: 02-Jan-2008
# Posted on: 05-Aug-2008 14:12:29   

Is it possible to save with savemulti() or something similar, data into an intermediate table. e.g. in a hypothetical example with author, title and authortitle entities, how do I save (update, delete and insert) the required records into authortitle table?

Note : There are some answers in the forums but I would like a new answer considering we are now in LLBLGen Pro version 2.6.

I am using version 2.6 final, .NET 3.5, self servicing two class template.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 05-Aug-2008 15:59:07   

I think the answer wouldn't be different than before.

manual wrote:

How do I create a m:n relation between two entity objects?

The generated code will not save intermediate table entries in an m:n relation. Consider the two entities Department (DepartmentID (PK, int, identity), Description) and Employee (EmployeeID (PK, int, identity), Name). These two entities have an m:n relation, and this results in the third entity DepartmentEmployees (DepartmentID (PK), EmployeeID (PK)). To relate a new employee with an existing department follow the example below. It will add a new employee to the existing department with ID 1. You can of course also set the DepartmentID of the DepartmentEmployeesEntity by hand, avoiding the necessity to fetch the DepartmentEntity first.

// SelfServicing

DepartmentEntity department = new DepartmentEntity(1); EmployeeEntity newEmployee = new EmployeeEntity(); newEmployee.Name = "John Doe"; DepartmentEmployeesEntity departmentEmployees = new DepartmentEmployeesEntity(); departmentEmployees.Department = department; departmentEmployees.Employee = employee; // save recursively departmentEmployees.Save(true);

// Adapter

DataAccessAdapter adapter = new DataAccessAdapter(); DepartmentEntity department = new DepartmentEntity(1); adapter.FetchEntity(department); EmployeeEntity newEmployee = new EmployeeEntity(); newEmployee.Name = "John Doe"; DepartmentEmployeesEntity departmentEmployees = new DepartmentEmployeesEntity(); departmentEmployees.Department = department; departmentEmployees.Employee = employee; // save recursively adapter.SaveEntity(departmentEmployees);

joshrivers
User
Posts: 10
Joined: 30-Dec-2008
# Posted on: 02-Mar-2009 05:35:04   

This can be an amazingly hard to find piece of documentation. I spent about 2 hours tonight trying to find the right words to put into Google to discover it.

It seems like what you are TRYING to do is add an item to a collection, delete an item from a collection, or save changes to a child collection in your entity class. However what you really want to do is manually manage the link table.

A useful additional thing to document (this is deducible from the "How Do I" but not necessarily obvious) would be How to delete a m:n relation between two entity objects:


DepartmentEntity department = new DepartmentEntity(1);
department.DepartmentEmployees[0].Delete();

ps. I know you know the documentation could use help. I'm mainly noting this here in hope of helping google find this for the next guy.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 02-Mar-2009 09:57:22   

Thanks for that.