Add a new property to a class in realm of react native - react-native

When adding a new property to a new class, the error shows "migration is required due to the following errors: Property "XXX" has been added to latest object model".

You have probably changed the db schema. If your in development stage just drop your database and delete everything. But if you are in production you should follow the migration guide by incrementing the db version number.
Check this out.

Related

How do I create a Realm DB for a React Native app?

Maybe I'm thinking of this the wrong way, but how do I create a pre-populated Realm DB? For example, let's say I want to create a dictionary with 1000 words and definitions in it. The user can change the definitions from within the app, but initially the DB will have default definitions.
Can I create a .realm file with the 1000 words and definitions and include it in my app?
FYI: I am using Realm with React Native and I am currently testing using emulator -avd CordovaAVD to launch my Android emulator.
I think I've figured this out so I'll post my solution in case anyone else might find it useful.
I have a function that will populate a Realm DB. Once I run that function, however, I want to grab that static DB and use it instead of generating the DB every time the app starts up. That's what prompted my effort. However, these steps will also be useful if you just want to back up a DB.
I am primarily testing using an emulator, but I think these steps will work if you test on an actual device.
To grab the Realm DB from the emulator:
Find the path to your realm file within the phone by adding this somewhere in your code:
let YourRealmDB = new Realm({schema: [YourSchema]});
console.log('YourRealmDB path =', YourRealmDB.path);
The path will be something like
/data/data/(package.name)/files/(filename).realm
"filename" will probably be "default"
From the command line, run
adb exec-out run-as (package.name) cat files/(filename).realm > (filename).realm
This will copy your Realm db to your current directory
Now, to use that DB in your app:
Create a realm object:
let YourRealmDB = new Realm({schema: [YourSchema]});
Set the objects path to your DB:
YourRealmDB.defaultPath = 'path/to/your/db/(filename).realm';
NOTE: If you use a 'local' DB like this and your app performs a write operation, it will not write
to your local DB. It will write to the db at YourRealmDB.path. So if you want to copy or
view the updated DB, you will need to run adb exec-out run-as (package.name) cat files/(filename).realm > (filename).realm
again to get the most current version of your DB.
I hope that helps. It took me quite a while to piece that all together.

Do I use Snapshot file, migration file or data annotations in my EF Core to update database?

I'm trying to understand the different types of migration paths we can choose when developing an ASP.NET Core 1.0 application with EF Core. When I created my first Core application I noticed it generated a ApplicationDbContextModelSnapshot class that uses a ModelBuilder to build the model.
Then I read that if I need to add a table to the database, I need to create the new model and run the command line to generate the migration file and update the database. Ok, I get it up to this point.
But when I do that, I notice that the ApplicationDbContextModelSnapshot class gets updated too.
1) Does that mean I cannot modify this ApplicationDbContextModelSnapshot class since it looks like it gets regenerated each time?
2) Should I use Data Annotations to build my model or should I use Fluent API which tells me to build my model in the ApplicationDbContext class? Huh? another file that builds the model?
I'm seeing three different ways of working with the database here, the snapshot class, data annotations, and fluent API. I'm confused because today, I made a mistake in my last migration file so I deleted the file, dropped the database and reran the database update.
But by doing that I got errors similar to:
The index 'IX_Transaction_GiftCardId' is dependent on column 'GiftCardId'.
ALTER TABLE ALTER COLUMN GiftCardId failed because one or more objects access this column.
So naturally I was wondering if I had to modify the ApplicationDbContextModelSnapshot class.
What is the path I should be taking when it comes to migrations or database updates because these three paths are confusing me.
I have run into this issue before when I create migrations, make model changes, create new migrations, and try to update the database. The root cause is when keys are being changed and relationships are not dropped and are not added back or do not exist.
You have two options
Easy Method
The easiest way is also the most destructive way and only possible in a dev environment.
Delete all migrations, drop the database, create new migrations and run 'update-database'.
Hard/Safest Method
This is the most time consuming method. I recommend do this in a local integration branch first, pushing it to a remote integration, and then production.
Open the migration file, ie 20160914173357_MyNewMigration.cs.
Drop all indexes in order
Drop/Add/Edit table schemas
Add all indexes back.
For either method, just be sure to test and test again.
Do not modify ApplicationDbContextModelSnapshot. It is a design-time artifact, and should only be modified in the case of a merge conflict.
To update the model, always use data annotations or the fluent API.
For more information on the EF Migrations workflow, see Code First Migrations. It's for EF6, but most of the information is still relevant.

Core Data lightweight migration error

I have been trying to get the core data lightweight migration working. Ran into an very difficult issue.
I have setup up automatic lightweight migration exactly like the documents and other SO posts. And then I create a new version, select it as current, add a new field to some entity, and then ran.
Got error:
migration failed with error Error Domain=NSCocoaErrorDomain Code=134140 "The operation couldn’t be completed. (Cocoa error 134140.)" UserInfo=0xce08c10 {reason=Can't find or automatically infer mapping model for migration,
One more error:
NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=134190 \"The operation couldn\U2019t be completed. (Cocoa error 134190.)\" UserInfo=0xcdf2d00 {reason=Each property must have a unique renaming identifier}";
I have googled for "Each property must have a unique renaming identifier" for a while and found no result at all.
From the meaning of this error message, the properties should have unique renaming identifier. I go back to my model setting and found that I did have some renaming identifier exactly same.
My question is I didn't really change the model name at all. I just add a new field. I thought renaming identifier will only be useful when trying to renaming something. Or maybe it's required no matter what. If that's the case, then why XCode doesn't give us any warning or error about it? shouldn't it be very obvious for XCode to see?
Any suggestion is welcome.
Answering to a post from the past, but you are not the only one who encountered this Can't find or automatically infer mapping model for migration problem.
So, I had the same problem, on a big project with 12 model versions, and only lightweight migrations. Just had 2 properties renamed, and everything was ok until today.
The only fix I found was to add a mapping model (New File => Core Data => Mapping Model ) to my project between my 2 last model versions.
I hope I won't have to add one for each new model update. Seems to be a migration bug in Core Data to me.
Hope this will help other people struggling with this error.
But happened on both iOS 8 / iOS 9 devices
I don't believe migration works on the simulator. I always have to delete the app from simulator and start over after making changes to the managed object model.
Test install on a device, which is already set up with the older version, to be sure your migration is successful.

Schema specified is not valid. Errors: 'System.Data.Spatial.DbGeography' which cannot be mapped to a primitive type

I'm doing Entity Framework (v6) db first. I get the following error when trying to access data from my datacontext.
The relationship 'Model.FK_Table1_Table2' was not loaded because the type 'Model.Table1' is not available.
The following information may be useful in resolving the previous error:
The property 'Location' on the type 'Model.Table1' has a property type of 'System.Data.Spatial.DbGeography' which cannot be mapped to a primitive type.
Any idea what is causing this error?
I don't know if this is your problem, but I had a similar error when I upgraded from EF5 to EF6. The spatial data types moved namespaces. This link has all the info:
http://msdn.microsoft.com/en-US/data/dn469466
Boiled down, you need to:
Install EF6
Update any namespace/using statements
Remove references to System.Data.Entity
(Probably) refresh your EF diagram.
Probably you need to see the solution on this url https://msdn.microsoft.com/en-US/data/dn469466
I have fixed same problem after updating some content of my code from mentioned url page there they have given solution for update to EF-5 to EF-6 after updating some of references on my context class. it is working fine.

How do I use DocumentDatabase.StartBackup() in conjunction with an RavenDB EmbeddableDocumentStore?

I read the Backup and Restore doc at RavenDB website and tried it in my code.
In the doc we have:
Initiating a backup
When running in embedded mode, all you need is to call the method
DocumentDatabase.StartBackup().
The above line of code just won't compile with error:
An object reference is required for the non-static field, method, or property
'Raven.Database.DocumentDatabase.StartBackup(string, bool)'
So I tried this just to test:
new DocumentDatabase(_documentStore.Configuration).
StartBackup(#"~\App_Data\Backup", true);
The code compiles but when the app starts I get this error:
Could not open transactional storage: C:\Project\trunk\MyApp\App_Data\Database\Data
Can someone please share some working code about how can one do a full backup of an online embedded document store? Is it even possible without using RavenDB server?
My only option is doing a manual backup of the database folder?
Leniel,
You need to use the StartBackup method on the actual instance that you are running.
If you are using the EmbeddableDocumentStore, you need to do:
embeddableDocumentStore.DocumentDatabase.StartBackup(...);