I have three entities like this.
Employee:
id name EmployeeDepartment: id departmentID employeeID Department: id name Employee<<---->EmployeeDepartment<---->>Department suppose Employee and Department has a Many-to-Many Relationship, I want to get all employee names in "Accounting" department? If I use SQL query, I will simply use: select employee.name from Employe开发者_如何学运维e, EmployeeDepartment, Department where employee.id = employeeDepartment.employeeID and Department.id = EmployeeDepartment.departmentID and Department.name = 'Accounting' However, how can I do the same thing in Core Data?Core Data is an object graph manager, not a relational database, so trying to force it into a relational DB mode will lead to pain. Your EmployeeDepartment entity is a perfect example of that; you need it in a relational DB to implement the many to many relationship between Employee and Department, but in Core Data it's not necessary at all. Instead, just give Employee a departments
property and Department an employees
property, and then add to-many relationships between the two entities.
Now, if you want to get all the employees in Accounting, you execute a fetch request for entity Department with a predicate that matches Accounting. Once you have that object, you can simply access its employees
property to get the list of employees.
精彩评论