Core Data Entity Inside Another Entity

So I have an entity in my core data model called recipe. I need to create another entity containing a recipe and a date that the recipe is assigned to. Can I do this similar to the way I've done it in the image and just save a Recipe object in the initialization of PlannedRecipe object in the Persistence file?

Basically I just need to know how to add a entity in an entity using this core data model and persistence file.

Persistence file:

import CoreData
struct PersistenceController {
    static let shared = PersistenceController()

    let container: NSPersistentCloudKitContainer

    init(inMemory: Bool = false) {
        container = NSPersistentCloudKitContainer(name: "ReciPrep")
        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        container.viewContext.automaticallyMergesChangesFromParent = true
    }

    func addPlannedRecipe(recipe: Recipe,date: Date, context: NSManagedObjectContext){
        let plannedRecipe = PlannedRecipe(context: context)
        plannedRecipe.id = UUID()
        plannedRecipe.date = Date()
        plannedRecipe.recipe = recipe //Giving me an error: "Cannot assign value of type 'Recipe' to type 'Data?'"
        
        save(context: context)
    }
    
    func save(context: NSManagedObjectContext){
        do {
            try context.save()
        } catch {
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
    }
}

I guess this problem can be solved if I can convert a recipe into binary data or some other savable data in an entity.

Any help would be greatly appreciated.

Accepted Answer

Most likely you want to use a relationship between a PlannedRecipe and a Recipe. Checkout this documentation: https://developer.apple.com/documentation/coredata/modeling_data/configuring_relationships

Not sure what the persistence file would look like but like @MatKuznik said you'd need a relationship. Remove the binary data recipe from PlannedRecipe. Then add a relationship (called recipe) to PlannedRecipe. In Recipe itself add a relationship to PlannedRecipe (I'll call it plans for now). Both will have to point to each other so switch the third tab to the other one so they point each way. On the far right pane you'll want to direct if it's a one-to-many (one-to-one or many-to-many) relationship by setting either side. I'd image a recipe can be in multiple planned recipes but a planned recipe can only have one plan? Unless your plan consists of multiple (WWDC example with pie crust, filling, and whip cream idea comes to mind) in which case you'd want a "to-many" on either side.

Core Data Entity Inside Another Entity
 
 
Q