Add new entity in already migrated core data - ios

i have a core data which has been migrated once before to add an entity. Now i am trying to add another new entity that will have relationship with previous entities, my question is that do i need to create a third model to add this new entity or i can use the second one?

If you have not released the second core data model beyond your development environment, then you can just modify the second model to include your new entity and its relationship. If you have released the second model in an app, then you will need to create a third model.

Related

Core data versioning/migration after deleting entities

I have a database which consists of 3 entities, say
'IronMan', 'CaptainAmerica' and 'SpiderMan'.
With new changes, I want to delete all three entities and create another entity called 'Thanos'.
I would not need to use any of the code and data stored earlier with entities 'IronMan', 'CaptainAmerica' and 'SpiderMan'. Do I need to do core data versioning or migration in this case?
As mentioned in Apple's documentation
https://developer.apple.com/documentation/coredata/using_lightweight_migration
You can add, remove, and rename entities in the hierarchy.
So, yes, you need to provide migration (new version model) but can stick to a light-weight migration, hence it will be done automatically based on changes done between the 2 models.

iOS - How to migrate 2 Entities into 1 using Core Data?

In my current model version I have this 4 Entities :
Satellite (with relationship to one to SatelliteAnimation)
SatelliteAnimation
Radar (with relationship to one to RadarAnimation)
RadarAnimation
In my App I have different blocs in which I display information.
This is why I have entities RadarAnimation and SatelliteAnimation, in order to recognize those blocs by type.
Anyway, my issue is that I want to migrate 2 blocs in 1.
And instead of having 4 Entities, I would like to have :
Radar
Satellite
Animations
I don't need the relationships anymore.
I know I can rename an Entity in a lightweight migration process.
What I would like to do is :
Create a new data model version
Delete the SatelliteAnimation entity
Delete the relationship between Satellite and SatelliteAnimation
Delete the relationship between Radar and RadarAnimation
Rename the entity RadarAnimation by Animations
Set the new data model version as current model version
Does this way of handling my issue will affect the migration process ?
Can I rename and remove relationships in a new data model version ?
This should be possible with automatic lightweight migration. Deleting entities and relationships just works with no extra steps, so that's fine.
To rename something you'll use the renaming identifier in the model editor. After you create the new version of your model, select the entity you want to rename in the model editor. Change its name but set the renaming identifier to the old name. In your case, change the entity name from RadarAnimation to Animation, but then also set the renaming identifier on the entity to RadarAnimation so Core Data knows what it used to be called.
Incidentally attributes and relationships can also have renaming identifiers, so they can also be renamed this way.
Apple provides detailed documentation on what can be done with lightweight migration which will probably be useful as you go through this.

Move a Core Data entity and its data into new Core Data model file through Migration

Is it possible to move a Core Data entity Car and its data in Model1 into a new Core Data model Model2 using migration? Model2 will also have a new CarOwner entity + other new relationships (so model file is different than Model1). Is this possible using lightweight migration or do I have to use custom migration? I'm using Magical Record to setup my Core Data Stack.
I have been using lightweight migration for years with success. So I don't have any custom migration mechanism in place. I want to ask first before I implement a new system so I can incorporate CD custom migration into my existing MagicalRecord Core Data stack.
EDIT: updated question to clarify that Model1 and Model2 have differences.
With the same xcdatamodel file and different versions you can use a Mapping Model file, but being different mom files i guess those are different stacks and migration will not work.
Model migration is only relevant if the model has changed-- meaning, if the entities contained in the data model don't match the entities saved in the persistent store file. Migration doesn't depend on what model file you use, it depends on the entity hashes contained in that model.
Meaning: If your new model file has exactly the same entities as the persistent store file has, you don't actually need to migrate anything. Just start using the new model file. However keep in mind that if you ditch the old model file and all of its old versions, you won't be able to migrate from older versions of that model any more.

Create Core Data entities dynamically during runtime

I need to be able to create new core data entities during runtime. I've written the code to create the objects programmatically, however, I can't add the entities during runtime as the model is immutable.
My problem is similar to this post, however there is no satisfactory answer: How to dyanmic create a new entity (table) via CoreData model?
The documentation regarding changing the core data model explains:
Managed object models are editable until they are used by an object
graph manager (a managed object context or a persistent store
coordinator). This allows you to create or modify them dynamically.
However, once a model is being used, it must not be changed. This is
enforced at runtime—when the object manager first fetches data using a
model, the whole of that model becomes uneditable. Any attempt to
mutate a model or any of its sub-objects after that point causes an
exception to be thrown. If you need to modify a model that is in use,
create a copy, modify the copy, and then discard the objects with the
old model.
However, I'm unclear on what exactly this is saying--that the whole core data model can't be changed once the persistent store coordinator has been used or the attributes/etc of the individual entities can't be changed.
To be clear, I do not want to change the attributes of my current entities, I simply want to add new entities. It just seems weird to me to have to use migration to add new entities.
Any thoughts?
Thanks!
The documentation is pretty clear.
Copy the model.
Apply your changes to the new copy.
Destroy your old MOC, Persistent Store Coordinator, and all objects created from those.
Apply a migration, if necessary.
Create a new Core Data Stack (MOC, PSC, etc) using your updated model.
The migration could be a sticking point, but it should be do-able.

How to use entity framework in other modules?

I am currently developing project using C# MVC and entity framework, I want to use the entity framework in other modules ,
i.e security module , Utility module ,
i want to call the db using the entity frame work, how do i do this ?
i am new to this are please explain in detail, idea is to break the project into presentation layer, business layer and data access layer..
i don't know how to archive this.
Try this way,
There are three ways to work around entity framework, Database First, Model First & Code First.
Database First: If you already have database, then entity framework can generate a data model that consists of classes & properties that correspond to existing database objects such as tables & columns.
The information of database structure, conceptual data model & mapping between them is store in the xml in an .edmx file.
Model First: If you don't have database, you can start creating model using vs entity framework designer. This approach also use the .edmx file.
Code First: In this approach, we don't need .edmx file. Mapping between store schema & conceptual data model is represented by code, handled by code convention & special mapping API.
Here I have used the Database First approach.
In order to use the Dal class lib, add the reference in the business logic layer and initialize the entities class. For example
Find the entity framework object.
Initialize the entity framework object in other class lib.
FrameworkEntities entities = new FrameworkEntities();
Please let me know, if you want to use model first or code first approach.
Get started from the below link for the Entity Frame Work
http://msdn.microsoft.com/en-US/data/ee712907?

Resources