I'm designing a DB using VS2012 (EF5) by first creating a model. I can't find a way to store images - not an image type, nor Binary type. What can I use?
If you were using data first, you would end up with a byte array (byte[]), so that's what I would go with.
Picking Binary as the type in the model builder with create a byte[].
Related
I have a iphone application in which I use nsecoding for saving my objects. However it has been decided that it should be saved using core data.
The questions is - what is the best way to convert my exsisting model classes to datacore classes. Should I create a new thorugh the xdatamodel editor and replace the old or what is the best strategy. I have not figure out an easy way to do this :P
Regards
Bjarke
Roughly, what you will need to do is this:
Create a data model that reflects the classes that you have already created - and I think you'll have to do it by hand in the model editor.
Then change your classes so they become subclasses of NSManagedObject. You'll have to make sure your properties are restricted to types handled by Core Data, of course.
I have an application that tracks a user and shows where they've been using MKOverlay. How can I save this information into Core Data so that when the user wants to see where they went yesterday they can load the map/overlay from Core Data?
I have a similar project. Mine is for cycle paths. Here is how I structure my core data model:
I use an order parameter so I can work out how the points connect up. But i think you can just check the 'ordered' property of the relationship now although im not entirely sure how it works. The min / max attributes are for more efficient searches. I store the lat long values as integers to save space after a suggestion to one of my posts. You might find this useful too.
You probably want to add some attributes to the Way such as Date.
You can save any object in a core data model, but if they are not the default type like string, int, etc. you won't be able to query on them.
So you have to construct your entity with property that you will be able to query.
So I see 2 options, you save every information in an entity, but this way you will need to alloc again all objects.
Or you only save the property you will need to query and archive your object in a transformable or in a Binary Data property.
I don't know what would be best.
How can I change the type of a Core Data entity column programmatically? For example, form String to Int 16. The entity can be assumed empty (no data row).
Firstly, it's not an SQL column, it's the attribute of an entity. Attributes have behaviors, columns in SQL do not.
In answer to your question, once a data model model has been used to write data to a persistent store file (sqlite store or other formats) it cannot altered programatically. If you change an attribute, you need to perform a version migration.
See: Core Data Model Versioning and Data Migration Programming Guide for details.
A little general advice: Core Data is not SQL. Entities are not tables. Objects are not rows. Attributes are not columns. Relationships are not joins. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.
You can manipulate the attributes (including) type of an entity in an NSManagedObjectModel until it is used to initialize a persistent store coordinator. So, create the NSManagedObjectModel, mutate the attribute (not a column) type, then setup the Core Data stack as usual.
Of course, if you already have any data persisted using the original model, you'll have to perform a schema migration to update the data to the new type.
We have a project which used Entity Framwork 4.0. We draw the model and then generate an SQL Compact 3.5 DB.
Since we needed a large BLOB store, we created a column with the Image data type (since Binary is limited to 8000 bytes). However, when updating to EF 4.1 our model was silently converted to having a Binary column instead!
No worries, we thought, we'll just change it back. Problem is Image cannot be selected anymore! And - specifying binary and setting length to a large value, say 100000, gives an error when trying to generate the DB.
We have found some pointers working with a code first approach, from EF 4.1 Release Notes, A related ADO.net team blog post, A third post describing the related issue and A SO questing discussing the related issue. However, all these talk about the issue using the Code First approach.
Any pointers on solving it Model First?
Thanks!
You need to open model file .edmx with XML editor. And in <edmx:StorageModels> section, change Type for your data Property from varbinary to image.
For example:
<EntityType Name="DataSet">
<Property Name="data" Type="image" Nullable="false" />
</EntityType>
You may also need to manually change the type of the column in .sdf file.
This is a little hack, so you have to do this every time you change your model.
convert your image to a byte array and store it like that then convert to image when you pull it out of the database that is how I store most of my blob type data
as far as the code first approach issues you mentioned is there a specific reason you cannot use code first API and fluent mapping with your database
Edit:
Use the varbinary(max) for datatypes larger than 8kb as image will be deprecated in the future ref
in your edmx file if you are using model first then you can set the column datatype in the properties section when the column is selected in the designer if you are using database first design then you can change the datatype in the database then regenerate model from the database
codefirst api create your data base then use reverse engineer codefirst using entity framework powertools ctp to generate your code first models and context
hi all just now i started database work for my app.my idea is to save some rectangles(x,y,width,hight individually)as int32 and nsstring as string upto that my work completed .Now my question here is, what is the use with the "transfarmable" type where we can use this. Thanks in advance for your answer.
I'm not sure about transfarmable, but Transformable is:
The idea behind transformable
attributes is that you access an
attribute as a non-standard type, but
behind the scenes Core Data uses an
instance of NSValueTransformer to
convert the attribute to and from an
instance of NSData. Core Data then
stores the data instance to the
persistent store.
By default, Core Data uses the
NSKeyedUnarchiveFromDataTransformerName
transformer, however you can specify
your own transformer if you want. If
you specify a custom transformer, it
must transform an instance of the
non-standard data type into an
instance of NSData and support reverse
transformation. You should not specify
a name if you are using the default
transformer.
as taken from http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html.
Essentially, if you want a custom construct to be serialized into the database and then reconstituted as your desired object/object graph, the Transformable type lets you access Core Data's framework for storing complex formats/objects in the database, letting you code the logic that does the serialization and deserialization.