modyfing a sqlite db mapped via core data throws errors: uncaught exception of class 'NSCFString' - cocoa

I have an iphone app that has a sqlite db, mapped to core data. All the data manipulation in the app is via Coredata.
On one table I wanted to add a string attribute. And I did two things both with similar crashes:
I tried to use Mesasql to alter the structure. I easily added a Varchar column. But it crashes.
On a separate attempt, with the mapped SQLlite db, restored, I tried adding a attribute to the entity in core data and it crashed.
This is the error I got:
* Terminating app due to uncaught exception of class 'NSCFString'
terminate called after throwing an instance of 'NSCFString'
So, how can I alter the structure of the sqlite table mapped to core data without breaking the app? Am I fogetting to do a commit or something like that?
Please help

Every time you modify an entity you have to delete the sqlite store. If your application has already shipped, the alternative is to migrate your data. This SO answer offers a simple explanation of how to do that.

Related

Easy Core Data migration during early development, when Data Loss is expected and acceptable

When a Core Data app is in early development, its Managed Object Model is often in constant flux. For each build, new Managed Objects and properties are added or deleted from the model.
When the Managed Object Model changes, the app will crash on next run with the error:
The model used to open the store is incompatible with the one used to create the store
The common advice in this situation is to delete the app from your device/simulator and re-run.
This works fine for developers using Xcode, but is annoying for non-technical stakeholders involved in the release process. It would be much preferable to not explain to the CEO or QA team that they must delete the app before installing that update from TestFlight. Or to field crashing bugs caused by this issue.
Once the models have been finalized a bit, we'll implement a real Core Data migration strategy.
In this dev phase, data loss is acceptable and expected.
This method will be removed before the app is released.
What's the easiest, lightweight, removable, debug way to "migrate" changes to the Managed Object Model between releases? This will likely be equivalent to "delete the app and re-run", but without needing to manually delete the app.
This should handle any and all changes to the Core Data stack, including adding and deleting Managed Objects and Properties.
In this scenario, I would check compatibility against the current model and then delete the SQLite database if a migration would have been required.
Consider using (in Objective-C)
// error, sourceStoreURL, theManagedObjectModel are valid
NSDictionary *storeMetadata=[NSPersistentStoreCoordinator metadataForPersistentStoreOfType: NSSQLiteStoreType
URL: sourceStoreURL error: &error];
BOOL storeIsCurrent=[theManagedObjectModel isConfiguration: nil
compatibleWithStoreMetadata: storeMetadata];
if (!storeIsCurrent)
{
// Alert user
// Delete on-disk store via sourceStoreURL
// (including -wal and -shm files if journaling enabled)
}
You can change the store URL when you change the model.
You can also do model versioning even for early development and then delete them all before you ship. This can also help your team learn the ins and outs of model versioning.

entity framework error after attached .mdf file

I attached an .mdf file in SQL Server 2008 and used that database for my entity framework database first project. Below is the error I got
Exception Details: System.Data.MappingException: Schema specified is not valid.
Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer"
I tried this link: How do I correctly set an association between two objects in the Entity Framework 4 Entitydesigner?
but it did not work for me. can anyone help me what can be the problem.
thanks,
michaeld
This exception usually occures if you have an entity in your model which doesn't mapped to a table (or an object) into the Database.
If you want that your project just starts debugging, Remove all entities from your model, Right-click in model designer and choose Update model from database
If you have an entity which it supposed to be mapped to a table into database, you should create a relative table in database, and map your entity to that table. You also can ghange your approach from db-first to code-first and enable migrations so that EF updates your db according to your model.
If you have an entity and you want to map it to a stored procedure in your db, see here

Changes in Model class causes drop database first and recreate, How to avoid this?

In Models context file it was mentioned
If you want Entity Framework to drop and regenerate your database
automatically whenever you change your model schema, add the following
code to the Application_Start method in your Global.asax file. Note:
this will destroy and re-create your database with every model change.
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<HMS.Models.HMSContext>());
so I added this code in the Global.asax file inside protected void Application_Start().
Adding this line drops whole database when I do any changes in model class. Is there any alternate way to avoid this and still I can do Model changes?
i.e After performing changes in model when I rebuild and run my application, it drops my database and regenerate all the empty model tables. So I loose all my entered data and I want to preserve table data.
If you're using EF Code First 4.1, then, no, it will only drop and recreate. EF version 5 apparently supports dynamically changing the underlying database. It's in beta at the moment, but is available for production use if you want to try it out. You can install it through NuGet.

InnerException {"Invalid object name 'dbo.Users'"}

I am currently trying to setup an ASP.Net MVC 3 Application for the first time. I know what I am doing with Ruby on Rails but completely lost with ASP.Net. I would like the app to create the database but not sure what I am doing wrong. I have an empty Database build called ssDB. I have setup the connectionStrings and built out the Model classes. According to everything I have read thus for, when I go to the run the App it should create the database but it doesn't look that way.
The Abbreviated Error I get is:
InnerException: System.Data.SqlClient.SqlException
Message=Invalid object name 'dbo.Users'.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=208
Procedure=""
Server=TESTSVR01
State=1
I have an empty Database build called ssDB
You have created an empty database with no tables. Since there is a database EF assumes tables are also created. Hence it will not proceed to create the tables.
You can drop the database and allow EF to create the database and the tables.
I also experienced the same issue while using Database first approach. Connection string in my service's/service's host web.config was incorrect. I corrected the connection string and it worked.

WP7 SQL Server CE How to Update

I'm updating my Windows Phone App to Mango and starting to use SQL Server CE. I can insert and select data but am struggling to understand how to update data.
Most of the examples are basic tutorials and only show inserting and retrieving data not updating. I believe I need to attach a class I have retrieved to the data context but that is as far as I have got.
Please can someone point me to an online tutorial showing how to do this. Or some example code would be great.
Thanks.
UPDATE
Basic updating looks fairly simple - query the database, update the object, call SubmitChanges.
My scenario is a little more complicated.
I have a page showing a list of jobs. A job (JobDetail) is selected and another page is used to edit the details. The job is written to a cache (another table JobCache) while the job is edited. If the user cancels the edit the cache is deleted. If the user saves the edit the JobCache object is pulled from the cache, converted to an object of type JobDetail and now I want to update the database with the new JobDetail object.I don't think I can just call SubmitChanges as the object was not generated from a query. I believe I need to somehow use Attach to do this. The question is how?
Why not get the original JobDetail object, update the changed properties from the cahced object and call SubmitChanges ?

Resources