Creating relationship in Core Data to perform deletion - ios

I am new to core data just started learning the new ideas in core data.
I have core data database which has three entity Student,Department and an entity for Mapping Student and department.Let name it as StudentDepartment
Student will have all student details with a primary key studentID
Department will have department details with a primary key departmentID
StudentDepartment will have studentID and DepartmentID as foreign key.
Multiple student can be enrolled in a department and a same student can be enrolled to multiple department.
How to create this schema in core data.
If am deleting a studentID in student table subsequent row should be deleted in StudentDepartment table. Similarly if am deleting departmentID in department table subsequent rows should be deleted in StudentDepartment.How to make this relationship by using core data.
Please provide me a xcmodel.

CoreData isn't a database, it's an object store that happens to (sometimes) be implemented on top of a relational database.
The practical result of that is that you really don't need to explicitly create a separate table for relationship mapping. Instead you create your two entities and then create a relationship between the two. From your description, it sounds like you want a many-to-many relationship between the two. At an implementation level, core data will magically create the needed relationship table.
Additionally, you can establish a delete-rule for each side of the relationship that mandates what to do when an item is deleted. Pin this case, you'll want to set the delete rule for both to nullify, which will break the relationship when either end is deleted.

Related

How to save array to an attribute of an entity?

I have a question about core data. I have an Entity named User and this entity has 3 attributes: name, images, videos. How can I save an array of multiple images or videos to the attributes images or videos?
With relational databases, whenever you have one attribute that is going to have a bunch of values for a single entity, you have a one to many relationship and you actually need another entity. So in a strict relational database, you would create a new entity, Image, that had one column pointing to the image's data and a second column pointing to the user that that image is associated with, a foreign key. In CoreData, they represent these foreign key columns as a relationship. So you'll have to make a new entity for each 1-to-M(any) relationship you have and give them a relationship back to user, and in turn user to them.
Try this tutorial, it may help you.

Core Data relationship between existing entities

I have two tables of data. One is table_A(id, x, x, b_id) and table_B(id, x).
I would like to add a relationship between b_id from table_A to id of table_B. I already have a JSON data like that, and I tried with Xcode to make so connection, but all I can make is a new relationship between those two.
I'm new to this, so would apreciate any help.
You are thinking of CoreData in terms of a DBMS which it is not. You don't need to set up foreign keys to make relationships in CoreData. If you want to assign a B table entity to a A you just create a relationship of between the two and you can set the attribute. The foreignKey and linking is all done by CoreData in the background.

CoreData relationship , with Delete

If I delete a CoreData record from an entity, does that also delete the relationship that record had with another record in another entity?
It depends on the Delete Rule that you have enabled for the relationship. The Core Data Programming Guide documents these (extracted below). Have a look in the inspector to see which option you have set for your relationships. If you have a specific data model which you want comment on you should edit your question to be more specific.
Deny
If there is at least one object at the relationship destination, then the source object cannot be deleted. For example, if you want to remove a department, you must ensure that all the employees in that department are first transferred elsewhere (or fired!) otherwise the department cannot be deleted.
Nullify
Set the inverse relationship for objects at the destination to null. For example, if you delete a department, set the department for all the current members to null. This only makes sense if the department relationship for an employee is optional, or if you ensure that you set a new department for each of the employees before the next save operation.
Cascade
Delete the objects at the destination of the relationship.
For example, if you delete a department, fire all the employees in that department at the same time.
No Action
Do nothing to the object at the destination of the relationship. For example, if you delete a department, leave all the employees as they are, even if they still believe they belong to that department.

Combine two table in one Entities Framework entity with DefiningQuery

I am having difficulties combining two entities into one where one is a DefiningQuery of readonly data.
I have a Person entity and a Company entity the Person entity is related to the Company entity throught the Company.CompanyID to the Person.CompanyID as one-to-many. The data for the Company comes from a different database so its represented as a DefiningQuery in my SSDL with a key. I want to make the fields in Company part of the Person entity by combining the entities.
Error 3024: Problem in mapping
fragments starting at line 445:Must
specify mapping for all key properties
(Person.PersonID) of the EntitySet
Person.
I assume the issue is that the Company entity does not have a PersonID but I don't want to make a Company a DefiningQuery with both PersonID and CompanyID
You have Person and Company in one-to-many relation and because of that you cannot map Person and Company fields into same entity. This type of mapping is called entity splitting and it requires one-to-one relation between tables which can be in EF defined only on shared primary key (because EF doesn't support unique constraints).

How to layout tables and relationships for MVC project

So I am working on an MVC project to put to work the studying I have been doing. I am wrestling with the concept of Database Table relationships and foreign keys. I am working on a simple ecommerce site (displays products, shopping cart, user accounts..etc).
I have the following tables to start out with:
1) Products
2) Categories
I setup the Products and Categories tables to have a ProductId and CategoryId respectively. In my MySQL db, I created a FK on the Products Table to relate to the CategoryId field on the Categories table (I am not sure this was correct to begin).
My expectations for the way the database would handle the table relationship: I didn't want the DB to do anything with the products table if I deleted a category out of the Categories table, or vise versa. The only thing would be that the category field in a Product would be blank (or default) if their category was removed.
Finally, do I have to do anything in my entity classes such as in the Products class, add the ProductId to the Category.ProductId?
Eventually, when I Orders and Users to the project, I can see a relationship where each user -> many orders -> each order has many products -> and each product is in one category.
But I am having a hard time understanding how or if I should be setting up a Foreign key relationship in the two current tables of Products and Categories and if so how to setup my entity class in relation to that FK.
Any advice.
It has been my experience (with L2S) that you don't want to specify any relationships between tables. I have simply done the PK, FK logic myself. This keeps the L2S generated objects simple and is most likely the way you worked with them before in SQL. That, at least, is the case for me.

Resources