Entity Framework, Oracle, DevArt, Context#ExecuteStoreQuery: System.Int32 constructed as System.Double? - entity-framework-4

I have an Entity-class having a Property of type Int32: on generating DDL using DevArt for ORACLE a NUMBER(10) column is generated. Reading and writing instances works flawlessly.
However, on fetching instances of this Entity-class sending a custom query to ExecuteStoreQuery on the ObjectContext this Property seems to be returned as System.Double, as such constructing the instances fails.
Can I hint DevArt to construct System.Int32?
Thank you.
Bart

The reason is the fact that OracleDataReader, which is used in the ExecuteStoreQuery method, has type mapping different from the one used in the Entity Framework provider.
I recommend you to use NumberMappings, I suppose you will need to map Number(10) to Int32: Number Mappings=((NUMBER,10,10,System.Int32). These changes should be persisted to the model connection string (they are duplicating the default EF mapping rules, it is necessary for the OracleDataReader from ExecuteStoreQuery).
Please let us know if the problem persists.

Related

SEGW entity type without key

I am trying to create a OData service that returns user parameters found in the databasde usr05. Those can be accessed by the ABAP statement:
GET PARAMETER ID 'XXX' FIELD YYY.
This means, that basically do not need a key value.
In SEGW it is impossible to create an entity type without one though.
The solution of course could be a dummy key field, that is simply ignored in the backend implementation.
Does anyone know a better solution?
Thanks in advance,
Eric
it is not a shame it is by design and follows the OData specification.
Every entity type must have a key.
What you can try to use instead is to use a property that is based on a so called complex type within an entity type that is used for your users.
The latter should have a key.
Like the complex type address that is used in the entity type Supplier
https://services.odata.org/OData/OData.svc/$metadata

Stored Procedures and Entity Framework 5

I'm using Entity Framework 5, and I reverse engineer code first the database I'm using, and then I added an ADO.NET Entity Data Model so that I can use Stored Procedures as reverse engineer code first didn't provide the use of sprocs. Is this the only way to access sprocs?
Also, I realize that after the reverse engineer code first process is done a bunch of classes (tables from the database) are created but as soon as I add the ADO.NET Entity Data Model, most of the classes go away. Does anybody know why?
DbContext.Database property exposes useful methods
http://msdn.microsoft.com/en-us/library/system.data.entity.database(v=vs.103).aspx
ExecuteSqlCommand( string, object[] )
http://msdn.microsoft.com/en-us/library/system.data.entity.database.executesqlcommand(v=vs.103).aspx
SqlQuery<TEntity>( string, object[] )
http://msdn.microsoft.com/en-us/library/gg696545(v=vs.103).aspx
There is a pattern that you can follow to create or support store procedures with the code first approach. here is a link that you can use to follow this:
http://www.codeproject.com/Articles/179481/Code-First-Stored-Procedures
In few words you need to do the same that you do with model first, create a class that supports the inputs and a class that supports the result set.
And about the Data Entity Model and missing classes. You need to consider that you only can have one approach in a project: code first/(model first/database first), so this could be the reason why you are not seeing those clases.
You can use Context.Database.SqlQuery to run SP.

Self Tracking entities and the mysterious ChangeTracker_ChangeTrackingEnabled datatable column

I'm using Self Tracking Entities that implements IObjectWithChangeTracker with the last Entity Framework RC available as a Nuget. The target database is PostgreSQL. I'm also using Code First fluent API to construct the model and LINQ to Entity for querying the database.
To my surpise, a simple SELECT query on the entity generates a SQL query with a mysterious column ChangeTracker_ChangeTrackingEnabled that does not exist in the datatable ! I do not understand this behavior as it seems to me that the EntityTypeConfiguration derived class maps the entity properties to the datatable columns in its constructor.
Is there a way to disable this behavior or at least tell which column should be mapped by the change tracker ?
For that purpose, Context.Configuration.AutoDetectChangesEnabled = false or calling IsConcurrencyToken() mapping in the EntityTypeConfiguration derived object does not help.
Any help appreciated.
TIA.
You must inform EF about every public property you want to avoid in mapping by either marking property with NotMapped attribute or by using Ignore in fluent API.
Btw. as I know STEs are not designed to be used with code first or DbContext API.

Property in Entity partial class

I have an entity/table that uses sqlgeography.
Since EF 4.X doesn't support spatial types I'm instead sending the bytes of the field back and forth.
I have stored procs on the database side that handles the converstion and properties on the code side to do that job.
To add the properties in the code I used a partial class.
One of those properties is for the SqlGeography which simply wraps around the byte[] property to handle getting and setting.
This property is hidden from EF using the NotMappedAttribute.
The other is the property exposing the byte[] itself and is decorated with the EdmScalarPropertyAttribute and DataMemberAttribute.
I then go to the EF model designer (*.edmx) to point the entity model at the Insert/Update/Delete stored procs.
It finds the stored procs alright and realises that they (when appropriate) take a VARBINARY parameter.
It also has a drop down allowing you to select a property on the entity class which maps to that parameter.
However this drop down doesn't list either of my properties. I don't care about the SqlGeography property since that is meant to be hidden from EF, however it is vital for me to be able to point it at the byte[] property, as that is where the data comes from.
I would very much like to avoid database triggers or wrapper classes and addiitonal fields to fudge this in to working.
I tried manually editing the .edmx file to include the byte[] property, but then it just complains it's unmapped.
Can anyone give me some insight in to how to get this to work? Or an alternative method of achiving the end result?
We could use a view to create the binary field for us, but this then involves manually creating a lot of the xml for the relationships within the data.
This pretty much voids the point of using EF which is to make life simple and easy.
For this project We'll just add a binary field to the table then have sprocs to handle the converstion on the server and a property in a partial entity class for exposing the geography type in the model.
Next project I doubt we'll be using EF. Dapper is so much more painless, even if theres a touch more code writing involved.
Here's the links for using views if anyone thinks it would be applicable to them:
http://thedatafarm.com/blog/data-access/yes-you-can-read-and-probably-write-spatial-data-with-entity-framework/
http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/
In the end we created a computed column for each table that exposes the spatial data as bytes.
We then use stored procs for inserting and updating the spatial data.

Entity Framework - Creating a strongly typed projection in the designer

I am trying to create a strongly typed projection entity "ProjectedPersons" via the designer in the Entity Framework.
I am using POCO classes and have seperated them out into seperate projects.
I keep getting an error:
error 3027: No mapping specified for the following EntitySet/AssociationSet - ProjectedPersons.
How can I remove this error. I only want to use the entity for projections not mapping to the database.
Thanks
If you are using the object only for projections - I would recommend creating it as a complex type instead. You get much of the same features without a lot of the contextual overhead that is inherent to an entity. I believe an entity is fully expecting to be mapped to a database feature in some respect - hence your error.
If you want to define projection in designer you must use QueryView (projection query defined directly in the designer and mapped to the new readonly entity) - that is the feature which must be manually defined directly in XML (MSL part) not in the designer. Otherwise you must do projection manually in Linq query. For linq query you don't need to create entity in the designer. You can use any class which is not mapped in the designer.
The QueryView was the solution. I found a helpful example.
http://msdn.microsoft.com/en-us/magazine/ee336312.aspx

Resources