Is there a way to map a Date column to a string property in EF? - entity-framework

I know I can solve this by adding another property to my entity which will contain the conversion between types but was wondering if it is possible with EF CF.

Do you need this for UI presentation (of the DateTime value) reasons?
If so, you might want to consider defining a separate View model class, and using something like AutoMapper to map the properties between the two.

Related

Map a table with a flattened collection back to a collection

I've got a table that I'm wanting to manually map via code-first for some read-only lookups. Currently the table has several sets of columns such as AGENT1_PROPERTY, AGENT1_PROPERTY2...AGENTX_PROPERTY1 is it possible to map these back to a array such that I have a property of AGENTS[X] on my entity?
There's no way to do this in EF that isn't way more work than it's worth. You could use a Value Conversion, or map a view as a Keyless Entity Type. But it will be easier to just map the Entity into a custom type and use that.

Core Data generic attribute type for abstract entity

I have an abstract parent entity Datapoint with a number of attributes, and 3 child entities BinaryPoint, FloatPoint, and ScalePoint. Each child should have a value attribute, but of a different type (bool, float, and int respectively).
It seems like bad design to do anything other than make it an inherited attribute, but I can't find a way to do this. Is there any way to make the attribute generic in the parent entity but not in its children?
Thanks in advance.
There is no way to do what you want to do in the Core Data Model Editor AFASIK. You'll need to pick a type of attribute, e.g. Binary Data, and handle the transformation from NSData to the actual value or viceversa yourself in code.

Entity framework 4 model first using money value object

I want to use a Money value object in my application. I have found several examples of a Money datatype. But I can't figure out how to use them with EF4. I would like to store each amount as a Decimal/CurrencyCode pair (where currencycode is a string - "USD", "SEK", etc) in the database. I tried creating a complexType but I couldn't get that to work. Is this possible?
It should be definitely possible. Your complex type is just pair of decimal and string property. It is exactly what complex type are used for. Depending on your approach you must do:
Database first:
You will define your database first. Your table will contain money and varchar columns representing your new type. When you update your EDMX model from database it will include it as scalar properties to your entity. You must remove those properties. Then go to model browser and create new complex type. Return back to entity and add complex property of your new complex type. And at the end you must go to entity mapping and map your complex type to those database columns.
Here is basic tutorial from MSDN but from unknown reason they didn't include such elementary details like screenshots. Here is some video from channel9.
Model first:
This is similar to database first but you don't have to deal with database creation and mapping. It will be generated for you.
Code first (EF 4.1):
You must create separate class for your complex type and use it as property in your entity. You should not need to map it by default - mapping should be infered. If it doesn't work you can map complext type either by using ComplextTypeAttribute annotation or by defining mapping in DbModelBuilder.
I can further extend approach you need to use if you provide more details.

Access the property used in mapping entity to a table in EFv4

When we have two entities in EFv4 EDM diagram and only one table for both in the database (for instance, having table Documents and entities Invoice and Qoute), table Documents having documentTypeId column as a discriminator and set this column as a discriminator in the EDM (in Table mappings), how do we read the value of this property in our code?
We cannot assign values to it because EF does it for us under the hood (based on what we entered in Table mappings for condition) but somehow I don't get it why we are also not allowed to read it.
Imo this property is already mapped so you can't map it again. It is used to determine type of materialized entity. Why do you need such column. Usually it is enough to use is operator like:
var document = context.Documents.GetById(id);
if (document is Invoice)
{
...
}
If you only need to select subtypes you can use OfType extension method like:
var invoices = context.Documents.OfType<Invoice>().ToList();
You also don't need to set this value when adding new entity because you are adding subtype - Invoice or Quote.
Edit:
As I understand from your comment you don't need this information in query. In such case you don't need to map it. Simply use partial class of your entity and add custom property which will return your string. Sound like stupid solution but actually it would be the easiest one.
Discriminator column should be part of mapping metadata so in case of T4 template generating your entities, it could be possible to update the template so it generate such property for you.
You may want to use a single-table inheritance hierarchy, as described here.
That way, you could have an abstract Document class that includes a DocumentTypeId column. Invoices and Quotes would extend this class, but specify certain DocumentTypeId filters. However, because the original class has a DocumentTypeId column, they would each have that column as well.
Another advantage to this approach is that you could create utility methods that can act on any Document, and you could pass any Invoice or Quote to these methods.

Map a property in the entity framework to a different type

I have a SQL Server 2008 database. I have a bunch of fields in TableA that are just strings that corresponds to booleans. So every value is either true or false. The edmx I generated using Entity Framework 4.0 has them as strings. This is technically correct but I would like to have them mapped as Booleans instead. Is this possible? If so how can I accomplish this?
Thanks much!
You could create a partial class alongside the generated one and add the bool property there with code to go back and forth from bool to string version. You could also mark the generated property as protected or internal to hide it from the rest of your code.
It's not ideal since the bool property cannot appear in query expressions unless you first force the query to happen using, for example, .ToList().
Your best bet would be to fix the database.