Use SwiftData for CRUD operations outside the SwiftUI view

Hi, I am trying out SwiftData but I have some problems. How to use ModelContext (or ModelContainer) outside the SwiftUI views. One option will be passing it as env variable. But it is not that great. If there is a bigger hierarchy, passing class through whole call hierarchy will be overkill. Or not - correct me if I'm wrong. So, if I want to store some object in some service class, how to achieve this? I have maybe wrong approach here as normally I am using EneitytFramework and/or raw SQL. Here I think I miss some knowledge.

One approach that I used was move creation of ModelContainer to some separate class and where it was instantiated.

Second problem is when I have to entities in relation one-to-many. When I fill all the data and try to store it, I get Fatal error: Unknown related type exception

Use SwiftData for CRUD operations outside the SwiftUI view
 
 
Q