I am trying to use automatic lightweight migration in my app. I did the following steps:
Create new model version.
Edit new model version.
Set the options NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption to YES upon creation of the persistentStoreCoordinator.
Setting the current version to the new version.
and got an error: reason = "Can't find model for source store"
I've tried Product->Clean to no avail. Ideas?
Edit: I forgot to put in the model details.
I have two .xcdatamodel files. The first has two Entities, Event and Venue. The second has one Entity, EventDate. I no longer use the second .xcdatamodel in code but I did leave the file in the project. I am trying to add a new entity Update to the first model file.
The error ""Can't find model for source store" means that the .xcdatamodel file originally used to create the existing persistent store cannot be found.
Supposed you started with these two model files (the version number is the version of the app not the data model):
oneEntityModelv1.xcdatamodel
twoEntityModelv1.xcdatamodel
… and you want to migrate to this data model:
threeEntityModelv2.xcdatamodel
The v2 version of your app would have to include all three files for the migration to take place.
The problem was that we were using the mergedModelFromBundles to instantiate our model. Apparently that doesn't play well with automatic lightweight migration. The fix was to
delete the unused xcdatamodel.
delete the database file.
version the remaining model.
Related
I have a custom entity data model extension that implements IModelGenerationExtension. Without getting into specific code, I have come across a situation where I get the "A model generation extension made changes to the model generated from the database that were not valid." when updating my model from the database. I am able to completely step through my extension using the Visual Studio experimental instance. The xml document representing the EDMX file modified by extension is valid xml. The error occurs after the scope leaves my OnAfterModelGenerated method. I'm assuming I did something the EF entity data model wizard doesn't like. How can I determine what the actual error is? I find that info for this sort of thing is not readily available. Any help would be appreciated. Thanks in advance.
You can save the resulting .edmx as a new file and open it in Visual Studio. It will show the errors it has.
Use OnAfterModelGenerated to edit the CurrentDocument only when WizardKind==WizardKind.Generate.
I know there is a lot of questions about Core Data Migration here in Stack Overflow but I couldn't find a solution for my problem.
I successfully did a Lightweight migration by renaming an attribute called "LastName" in the source model to "Lastname" in the destination model. I did step by step to achieve this and it works except the fact that all of the previous data on the column "Lastname" now is lost. The new renamed attribute exists, i can insert new Entities and the values of the new inserted entities are ok. The problem holds on the old data and the old attribute. As apple documentation says, I put the renaming identifier of the new renamed attribute on the destination model to the name of the old attribute in the source model by XCode interface or by code and none works. My data on the renamed attribute is always lost.
Is that expected or a miss something?
Thnxs,
I recently had this same issue where even with a renaming identifier, the data was being lost between the versions of my model. The model was jumping from Version 10 to 15 (with the change happening between versions 11 and 12), so the renaming identifier wasn't being pulled out, even when I included it in later versions. I also had the issue of how to deal with any later changes to the attribute.
In the end, I added the following code to the middle of the persistentStoreCoordinator method in the App Delegate (which is part of Apple's placeholder code when you setup a new project with Core Data added) before you connect the persistent store to the persistent store coordinator to perform lightweight migration:
NSEntityDescription *entity = [[[self managedObjectModel] entitiesByName] objectForKey:#"EntityName"];
NSPropertyDescription *property = [[entity attributesByName] objectForKey:#"NewAttributeName"];
[property setRenamingIdentifier:#"OldAttributeName"];
I then used this question on StackOverflow (CoreData how to detect new version of an app so I can update the model?), which describes how to store the CFBundleVersion alongside the persistent store. That way, if you update the attribute's name again, you can check the current version of the persistent store and you know the latest version of the store, so you can choose the correct renaming identifier on the fly.
I have several columns that I changed from Int to BigInt.
I opened up my EF model and did an 'Update Model from Database' and expected to see those columns now be Int64s. But they are still Int32s.
(I ran it several times just to be sure.)
I double checked my database and the columns are definitely BigInts.
So... does 'Update Model from Database' not work for a change of data type? Does it need to be manually applied?
Unfortunately, you'll need to delete the items from your model and then add them back in - at least that's the only thing I have managed to get working.
There is at least one third-party tool that is supposed to help with this, but have not tried it personally.
I'm using VS2008 SP1. If you change the datatype in the "ModelView" (CSDL) of the edmx, errors will occur because the "DatabaseModel" (SSDL) is not updated. You have to edit the *.edmx manually (XML). That is not so hard as it sounds.
You can simply search for the Property that the "Error List" of VS provides you (search in files is maybe the best solution for this). Go to the line where the wrong datatype appear and fix it.
e.g. you changed float to nvarchar(50) on the database --> go to your model and change Double to String --> validate --> Error.... --> Search for the property and make following changes:
<Property Name="YourChangedProperty" Type="float" />
to
<Property Name="YourChangedProperty" Type="nvarchar" MaxLength="50" />
This works very well if you know exactly what you've changed on the database. If you've made countless changes, you would have to analyse your changes with some DB-compare tool or regenerate the whole model.
Not very nice. But it "works".
take care
M
Correct - data types don't appear to update automatically. You can simply change the data type in the model view using the Properties window and change the Type to Int64.
Answer
For the specific scenario you mentioned, you will need to manually change the Type from Int32 to Int64.
There are a number of ways that this can be done, but the easiest is probably just to open the model (using the default editor) and change the Type of the property from Int32 to Int64.
Explanation (from MSDN Library)
The ADO.NET Entity Data Model Designer (Entity Designer) uses the Update Model Wizard to update an .edmx file from changes made to the database. The Update Model Wizard overwrites the storage model as part of this process. The Update Model Wizard also makes some changes to the conceptual model and mappings, but it only makes these changes when objects are added to the database. For example, new entity types are added to the conceptual model when tables are added to the database, and new properties are added to entity types when columns are added to a table. For details about what changes are made to the .edmx file, see Changes Made to an .edmx File by the Update Model Wizard.
For your scenario, the important thing to note is that the update model wizard is updating the .edmx file with your changes, but only to the storage model. When changes are made to the definition of existing columns, the conceptual model is not updated. For a complete description of changes made by the update model wizard, please see the "Changes Made to an .edmx File by the Update Model Wizard" link above.
You need delete your EF model and than create again and will work.
This may be an older question, but it is most assuredly still relevant today as the issue has not changed. As such, I thought I'd offer a synopsis of the research performed to date on the issue, including some observations of my own:
This failure to update the existing data-types is by design, based upon notes attached to Microsoft's documentation on the topic, as reported in the answer from timb. (Note that Microsoft appears to have either moved or purged the linked document and current documentation does not refer to this issue, but similarly worded notes can still be found elsewhere in archived documentation. Reference) The "Update Model from Database..." wizard does not update those changes, and instead pushes the onus for resolving the issue back onto the developer, in order to avoid making incorrect automatic changes which could theoretically corrupt the Model in ways which were not intended by the developer.
As such, there are really only two ways to resolve the issue, without resorting to third party tools:
Delete the affected tables from the Model and re-add them with the "Update Model from Database..." wizard, as noted in the answer from E.J. Brennan. The potential weakness of this method is that it doesn't always succeed, particularly if the original model was generated using a previous version of the Entity Framework, which can sometimes force the developer to perform significantly more work than might otherwise be necessary to complete the task.
Manually adjust the affected fields from the graphical model viewer, by right-clicking on the field in the table and selecting "Properties..." from the contextual menu. NOTE: Do not make manual changes directly to the Model .cs files from a code window, as such changes will be reverted the next time the "Update Model from Database..." wizard is run; if the changes are performed from the graphical model viewer, they will persist beyond re-running the wizard.
Also if you work with MySQL on Windows - recreating model may also wont help. Schema is somehow cached in MySQL. So if model is not updated even after recreation try to restart MySQL service and VS to be sure. After that model should be successfully updated.
I solved this by modifying the .EDMX file with a text editor.
Find your value and change it's type. Then correct the type in other errors that will be shown in the debugger.
I have made a data model, and now I've made a 2nd version. All of the generated NSManagedObjects mapped to that data model are all version 1. Is there some way to update them to v2 without deleting them and then saying having them generated again?
If you have two versions of your model you need to either create a mapping model between the two versions or you need to turn on automatic migration if the changes are simple enough for Core Data to resolve.
I would suggest reviewing Apple's documentation on the subject of Core Data migration.
update
I'm using auto migration. But say I have a person entry in v1 that just is last name and first name. In v2 I added date of birth. After making the data model of v1 I told Xcode to generate the NSManaged objects (in this example Person.m) now I've made v2 model with auto migration set up. How do I update Person.m to add DOB without deleting and then adding it again, or adding DOB manually?
The NSManagedObject will already have it in there, if you want to add the properties (as opposed to just the attributes) then just type in the two lines of code into your NSManagedObject subclass.
update
Other option, which I highly recommend, is to use mogenerator instead of the built-in code generator. mogenerator will keep the files up to date after model changes.
I'm using .net framework 3.5 SP1.
After adding a column to one table in Sql Server (as well as changing an existing column from allowing nulls to not nullable), I can no longer run my project without getting this error:
The number of members in the
conceptual type
'XBRLDimensionalModel.axis_t' does not
match with the number of members on
the object side type
'EOL.Xbrl.Persistence.Data.axis_t'.
Make sure the number of members are
the same.
I gave up trying to find and fix the generated code. I now have deleted all my local entity-related files and re-generated them by starting over from scratch and adding a new item (ADO.NET Entity Data Model). I still get this error.
The only way I can run the project now is to undo all my pending changes and use the last version from source control, and of course change the two modified database columns to nullable.
From all I've read so far it seems like I simply should have been able to "update" my model from the database. That resulted in this exception (above). But now I'm totally confused that even with a complete regeneration of the entity model and supporting classes I'm still getting that error.
I changed the property on my edmx model: "Metadata Artifact Processing" to "Copy to Output Directory". The Designer.cs, csdl, msl, ssdl files all seem to be consistent with the latest DB changes.
The exception is being thrown the first time my entityModel instance is referenced. So it is prior to any loading or saving of the data from the changed table.
any ideas where I'm going wrong?
Thanks,
TG
This error can also happen if the EDMX file was changed outside Visual Studio. Right click on the EDMX file and click "Run Custom Tool"
Open your model as XML. Remove all references to that type from the CSDL. Save and close, then reopen in the GUI. Now you should be able to update model as usual. If that doesn't work, do the same thing, but remove from MSL as well.
This seems a little verbose for a comment so I'm adding this as another answer:
In response Craig's suggestion I opened the edmx file in an XML viewer and removed all references to Axis_t (including the associations due to Foreign Keys). From the entire file.
I then "updated" the model by opening the edmx file as the GUI interface, right-click | refresh from database | Add (tab) which now only lists the Axis_t table. I added the table which seemed to work fine and included my new column and the column was mapped correctly.
I then ran the project to the same result. Same error as posted above.
I have now reverted back to what was in source control as well as changing the database columns (new one and modified one) as nullable. The project runs fine. I still have not successfully been able to implement the new DB column in EF. It behaves as if there is some stored/compiled version of the model which is not being updated via the "update" process.
When changing a foreign key from nullable to non-nullable (or vice-versa), be sure to change the association multiplicity from 0..1 to 1 (or vice-versa). The designer sometimes misses this in an update from the database.
Right click on your edmx file and open with XML.
Find the incorrect data types and change them.
Save file.
This worked for me.
I was facing the same issue, when I added one column to db and added respective property to entity(edmx).
I was also using source control for my solution. As I noticed, while saving changes to any file, save action prompts with overwrite option.
So, I overwritten the designer.cs as well config file and problem gets resolved.
regards,
bhushan
This has happened to me when I am working on two versions of the same application (with Model differences) that compile into the same folders.
It appears that Visual Studio doesn't "clean out" the "obj" temp folder properly and some fragments of the old model are still in there.
If I simply delete all files from the "obj" folder and re-compile this error goes away.
The Model in each version is perfect which was driving me crazy.
Not saying this is the answer to the OP's question but it is definately another reason for this error.
Steve
I had this same issue for hours. Found that in my Designer.cs file one of the properties of my entity was missing its attributes. (Don't know how this happened?)
[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = false)]
[DataMemberAttribute()]
public global::System.Int32 ContractCapacity
Now the error message makes sense, this was the missing DataMember. Once the attribute was added IT WORKED!!!
Here's a nightmare scenario that I just experienced: I have an MVC2 website and a WCF service that are built separately, but share a configuration. Within both projects, I used the same Entity Container Name; therefore I picked up the same connection string for both projects. Eventually the metadata got out of sync between the two and caused this error. The obvious solution was to not use the same Entity Container Name in the two projects; changing to a different name allowed me to specify unique connection strings, and therefore metadata, for each component which avoided the problem.
Now that I figured it out, it's obvious, but I had a tense hour or two!