+entityForName: could not locate an entity named 'Dogs' in this model. - ios

A little question regarding Core data models.
Here is why I'm posting : +entityForName: could not locate an entity named 'Dogs' in this model.
What I wanna do :
I wanna create an app that will read/write in core data details about animals with 2 entities Cats and Dogs.
-For that I create a window-based project with "use core data" checked.
-Then I create my views, and click on "animals.xcdatamodeld" file to create a first entity called Cats. I add the attributes, generate the class "cats.h" and "cats.m".
- I write down the code to read and write on this entity => No problem everything works well.
At this point I made a mistake by deleting the file "animals.xcdatamodeld" manually... I recreate it with the same name and add a new entity Dogs with its attributes and generate the associated class.
That's where the problem is, when I build and run the error message comes : +entityForName: could not locate an entity named 'Dogs' in this model. FYI, the read/write on Cats is still ok.
I have no idea why this error appears... I did not change anything on the App Delegate. I found very little information online related to that issue.
This only thing I found was that maybe the model needs to be versioned when updated => Can someone explain that a lil bit please ?
Anyway the model does not seem to have been updated when I added the new entity...
Thx.

Ok as expected somehow, this error was due to a stupid mistake...
The deleted xcdatamodeld file was replaced by a new one with a different spelling...
Now everything's fine...

Related

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.

Entity data framework in MVC - recurrent error database-first model update

This is probably the single biggest time-waster I have: I must have wasted cumulative days trying to get round it.
I'm using Visual Studio 2015 and ASP.NET MVC 5, but don't think this is important - I've had this problem in other versions. I accept that it's based on my ignorance.
I've got a reasonably complicated SQL Server database, and am using database-first model generation. To keep things manageable, I've got about 20 different models, each containing tables on a particular theme.
So let's say I want to generate a model to contain tables to do with the maps in my database. First thing to do is to add a new entity model database:
I want to call my model webMap:
I choose to generate the model from my database:
I've got a perfectly good connection string to the database in my web.config file, so I use that and choose not to save this:
I then choose the tables for which I want to generate classes, and these options:
What happens then is that it doesn't recognise the new database context:
The reason is (I think) in this file:
This has created a class named after a connection string:
If I change the partial class and constructor name like this:
then it solves the problem - until I next need to update the database.
Please forgive my ignorance about what's going on. Although I like what entity frameworks do, I find the opacity of what's going on hard to work with. Could any kind person tell me what I'm doing wrong, without delving into T4 transformations? I've tried cleaning my solution and running custom tools, by the way. Thank you!
Open the edmx file in the designer and click somewhere inside to select the conceptual model in the Properties window. Now look at Entity Container Name property. The value should be Entities - I think derived from your connection string name and no way to be specified during the Add wizard steps.
Simply change it to the desired name of the context (like webMap), save/build and you are done.
But make sure the Namespace and Entity Container Name property values are different.
Many thanks to Ivan and garret for their answers. I've collated the information and put here a procedure which worked for me. I don't claim to understand fully what's going on (but surely the point of a framework like this is that I shouldn't need to?).
Here's how I managed to create my model. There are 6 steps. I don't know which of them can be omitted, but following them all solved my problem.
Step 1 - Delete any existing models referencing tblMap
I found that I had inadvertently created another model. Doing a global search for (in my case) webMap can help find if you are in the same position.
Step 2 - clear the web.config file connection strings
I don't understand why, but it appears that the connection strings in your web.config file show up as conflicting names in code. So I did a search for webMap in my web.config file and deleted all connection strings containing it. I think this is the step that I had previously omitted.
Step 3 - Clean and rebuild the solution
I cleaned the solution (no idea if this was necessary), then rebuilt it to ensure that the only errors I had left were in my code referencing the webMap database context which no longer existed.
Step 4 - Follow the steps to add an ADO entity model
But ... call the item name in the first screen something like webMapModel, the connection string something like webMapEntities (and choose to save this in the web.config file) and the namespace something like webMapNamespace. Note that none of these is the webMap name I want to end up with.
Step 5 - Change the entity container name
Double-click on the model .edmx file which has been created to open it (this might not be necessary - it may be open already). Click on the white background of the model to deselect any entities. Press F4 to bring up properties.
Change the entity container name to webMap as shown above.
Step 6 - Rebuild your solution
At this point all my errors disappeared!
Name of Your context, should be different than namespace.
Try to use this configuration:
And in next screen:
And yes, it will add next connection string to web.config, but it's ok.

Entity Framework Multiple EDMX file sharing same tables

Environment : EF 6, SQL 2012
Setup: Database First, LazyLoading disabled
The question might appear more generic but will try to explain it in the best possible way.
I have a large application using ASP.NET MVC and grouped the entity based on the logical functionality. Hence we built multiple EDMX files
There is scenario in which we have to use the similar entity in two EDMX file.
School has relation to Teachers and Students.In first EDMX file, i used school and Teachers.In Second EDMX file, i used school and students
But only one Entity class getting created. If i run the custom tool on second EDMX context file, then the entity(school.cs) on my first edmx getting disappeared and it appears on the second one..
Why this strange behaviour occurs?
Here is the code in my first EDMX file
As you see here, i m not accessing school entity and also i disabled Lazyloading. But it complains that it couldnt find school file. Note: Courses has navigation property to school. But i didnt include it here.. Why its occuring so?
var courses= DB.courses
.AsNoTracking()
.Select(e =>
new CourseDTO()
{
CourseID= e.CourseID,
Name= e.CourseName,
Desc= e.Desc,
isActive= e.isActive
})
.OrderBy(e => e.CourseID);
The problem is, I m able to include one entity in the EDMX file only..
In first EDMX, it has navigation property to Teachers
In second EDMX , it has navigation property related to Students. But only Entity file exists at a time.. With only one Entity file, the code breaks
Note: This is just sample..Not my original application
Thanks #GertArnold. Meanwhile, i tried to create folders and kept the EDMX file inside it. Means i created seperate folder for each logical group and then included edmx file inside it. This in turn made the edmx file entities have different name space(i mean the entity classes) and also it enabled to have the same entity across multiple EDMX files. It sounds to have resolved my problem.
I didnt try to have them included under different namespace.. The whole idea started when i realized that even though i have two EDMX files, the associated entities(.csfiles) are created in the same physical location. I tried to create sub folders and included the EDMX files. It resolved the problem and i found it is having different name space
:):)

Core Data: "EXC_BAD_ACCESS" when using plain int data type

I have some doubts regarding storing plain "int" or "short" (int16_t) attributes inside the "Core Data" data management framework (on iOS 6 in my case).
I tried using it various ways and found it impossible to do, failing with the above mentioned exception. Now, a few weeks later, I found this article inside StackOverflow and it looks like someone does exactly that:
See following example for storing an Enumeration in the lower answer from "Daniel Eggert":
Best way to implement Enums with Core Data
Here it looks as if he uses a plain "int16_t" and maps it to a "Core Data" entry "Integer 16" inside the data model... does this really work? I tried it exactly as stated (even using the same naming :-)) and it fails with the well known "EXC_BAD_ACCESS" exception... as expected.
Any thoughts on this one? Did I misinterpret the linked answer?
---- Edit: ---
Interesting how someone rates this question (that even properly links another question) down, but doesn't have an answer?! Anyway:
I now tried the very same with the "Event" sample application from Apple and it works.
My own test doesn't work, still. Main difference between the apps:
My managed class is not auto-generated, I reused an existing class
I still have some properties with "#synthesize" in my existing class
My own entity (managed object):
h:
#interface TestEntity : NSManagedObject
{
}
#property (nonatomic) int64_t testAttribute;
#end
m:
#dynamic testAttribute;
model data type: "Integer 64".
other.m:
TestEntity *testEntity = (TestEntity *)[NSEntityDescription insertNewObjectForEntityForName:#"Trip" inManagedObjectContext:[GenericDAO getManagedContext]];
[testEntity setTestAttribute : 4]; //this triggers the exception
After going into detail with the whole issue the last week, I found out that using iOS 6 and the latest XCode (4.6.2 in my case) I can do everything exactly as specified in the link mentioned in my question. The issues I have seen are related to a set of technical details that are simply not documented. How did I find out about it? I simply did the following steps to validate my entities:
Create a new empty project with Xcode that support Core Data
Use the "editor" menu item "import" when being in Xcode, having the data model open (select the "XY.xcdatamodeld" you want to use with Core-Data.
You will see which items are imported in your model and which are "incorrectly" specified (either by property settings or else)
Now simply create all attributes you originally wanted to have coded as primitive data types, and select the "export" menu item inside the "editor" menu (when data model is selected!). Careful: check the "create primitive data type" check box to get base-types instead of Cocoa/object properties.
To check back your original project, do this also in the original project, in my case Core-Data and even the menu items looked differently (some options are greyed-out):
Do the same steps (importing entities into the data model) in old/your current project.
If the result is different, this will tell you that your project settings are somehow incompatible/old and you can go change the project settings to find out which ones impact the Core Data behavior.
Also try to use the export function to see if it generates the code correctly.
Other important tip: you can select the attributes in the Core-Data data-model editor and copy/paste them between projects.. This comes very handy if you want to migrate some entities/properties for testing. Do not just copy over the whole "cxdatamodeld" settings file, it keeps its settings from the old project (I tried it two times to validate that).
Check you set the Name, Class and Parent Entity correctly for all the entities in your Core Data Model file. Check all your class names match your entity names. The reason for the problem is when compiling the model it doesn't find the header file with the setter.

Code First and Model First in the same project?

I have two databases that I am accessing. The first is against a contact database which I connected to using EF Model First; creating the edmx. I have since begun to learn the virtue of CODE First when working with Entity Framework, so I decided I would, in the same project, write the Product database using Code First techniques, allowing the database to be generated from the code I am writing.
Everything compiles fine. The problem occurs when I hit my harness and it attempts to create the Product database and retreive a list of values from one of the tables...
I get the folowing error "Could not find the conceptual model type for 'Core.Data.Account'", when I attempt to enumerate the ProductLines property (Line3 below).
1. using (var ctx = new ProductDb())
2. {
3. var lines = ctx.ProductLines.ToList();
4. this.litOne.Text = lines.Count.ToString();
5. }
After some research it appears that this message may be occuring because of multiple entities with the same name (regardless of namespace), however there is nothing in the ProductDb context with the name "Account".
There is a class in the OTHER context created by the Model First approach named "Account". But how/why would that make a difference? They each point to different databases i.e. different connection strings. Why would the ProductDb be attempting to create a table called Account, when it should be completely unaware of it's exstence?
thoughts?
Thank you as always!,
- G
I bumped into the same problem, but the other way around: first a DbContext + generated database and then generated an edmx off the database (just for a little presentation). It appeared to be a restriction in EF: EF currently has a restriction that POCO classes can't be loaded from an assembly that contains classes with the EF attributes.
The only thing you can do for now is keep the contexts in separate assemblies.

Resources