Error creating OData Web API Controller for Complex T ype

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
rmagruder
Posts: 47
Joined: Thu 26 Jun 2014 17:12

Error creating OData Web API Controller for Complex T ype

Post by rmagruder » Fri 27 Jun 2014 17:48

I am making an OData v4 Service on top of a Model-First Entity Model (edml). I have a class that has a property called "RedemptionTimeRange" of ComplexType: TimeRange, defined as having a StartTime and EndTime. I went to add a Controller to it in the OData service Controllers folder, and pointed it at this type, but the scaffolding generation code gave me this:

Error

There was an error running the selected code generator:
'Unable to retrieve metadata for 'Redemption.Entities.TimeRange'. The type 'TimeRange' has already been configured as an entity type. It cannot be reconfigured as a complex type.'

Can you point to where I went wrong? It IS a complex type in the EDML...what exactly is the source of this exception? Entity Framework? The Visual Studio scaffolding code?

Any help in figuring out where things went south would be appreciated.

rmagruder
Posts: 47
Joined: Thu 26 Jun 2014 17:12

Re: Error creating OData Web API Controller for Complex T ype

Post by rmagruder » Fri 27 Jun 2014 19:31

For the heck of it, I turned off the Data Annotations in generation, and just had it generate the DbContext classes with pretty much all default template generation options. It got worse. Pointing at my Complex Type for a controller I now get the message below. But what really stinks is that these entities DO have entity keys clearly defined and marked as such, but since I didn't generate data annotations they don't have [Key] attributes on them.

There was an error running the selected code generator:

'Unable to retrieve metadata for 'Redemption.Entities.TimeRange'. One or more validation errors were detected during model generation:



CouponValueBase: : EntityType 'CouponValueBase' has no key defined. Define the key for this EntityType.

PurchaseTypeBase: : EntityType 'PurchaseTypeBase' has no key defined. Define the key for this EntityType.

TimeRange: : EntityType 'TimeRange' has no key defined. Define the key for this EntityType.

Product: : EntityType 'Product' has no key defined. Define the key for this EntityType.

CouponValueBases: EntityType: EntitySet 'CouponValueBases' is based on type 'CouponValueBase' that has no keys defined.

PurchaseTypeBases: EntityType: EntitySet 'PurchaseTypeBases' is based on type 'PurchaseTypeBase' that has no keys defined.

TimeRanges: EntityType: EntitySet 'TimeRanges' is based on type 'TimeRange' that has no keys defined.

Products: EntityType: EntitySet 'Products' is based on type 'Product' that has no keys defined.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Error creating OData Web API Controller for Complex T ype

Post by Shalex » Mon 30 Jun 2014 07:33

rmagruder wrote:There was an error running the selected code generator:
'Unable to retrieve metadata for 'Redemption.Entities.TimeRange'. The type 'TimeRange' has already been configured as an entity type. It cannot be reconfigured as a complex type.'
Please send us a small test project and specify the exact steps we should follow to reproduce the error in our environment.

rmagruder
Posts: 47
Joined: Thu 26 Jun 2014 17:12

Re: Error creating OData Web API Controller for Complex T ype

Post by rmagruder » Fri 25 Jul 2014 15:43

I ended up scrapping model first and re-doing the project as Code First.

I had a really hard time building an easy, small project due to time constraints.

Model First was basically nothing but frustration - it forced me to spend a lot of time fighting with cardinality of relationships, foreign keys, etc instead of just designing my classes and letting convention figure out how to build the database.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Error creating OData Web API Controller for Complex T ype

Post by Shalex » Mon 28 Jul 2014 11:05

rmagruder wrote:Model First was basically nothing but frustration - it forced me to spend a lot of time fighting with cardinality of relationships, foreign keys, etc instead of just designing my classes and letting convention figure out how to build the database.
If you switch to Model-First approach, be aware of the following option which allows applying default mapping in SSDL automatically: Model Settings > Synchronization > Mapping > "Enable automatic synchronization of storage part and mapping of the model with its conceptual part". Have you employed this option? Were there any problems with automatic mapping?

rmagruder
Posts: 47
Joined: Thu 26 Jun 2014 17:12

Re: Error creating OData Web API Controller for Complex T ype

Post by rmagruder » Tue 29 Jul 2014 17:53

I dumped Model-First. It was just too troublesome.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Error creating OData Web API Controller for Complex T ype

Post by Shalex » Wed 30 Jul 2014 06:55

Model-First approach should be easy enough to implement.
If you encounter any particular difficulty, please send us a test model so that we can reproduce the issue and help you to solve it.

rmagruder
Posts: 47
Joined: Thu 26 Jun 2014 17:12

Re: Error creating OData Web API Controller for Complex T ype

Post by rmagruder » Wed 30 Jul 2014 13:29

The problems I had with Model-First I believe were Entity Framework issues, not DevArt issues.

Creating 1:1 relationships, in particular, was troublesome with the model. It kept insisting that the cardinality be different than I wanted ('must be *'). Cascading deletes wouldn't work right, and so on. Ultimately, it was just easier to write the code that expressed the one-to-one relationship and let Code First figure it out.

The error messages that come out of EF when a model doesn't validate are indecipherable, even if you speak perfect English! It's just very, very picky and very, very database oriented, rather than model oriented.

I'm sure, in the end, it was user error on my part, but I couldn't figure out why something conceptually so simple could cause so many errors and roadblocks in EF, when Code First had NO problem figuring it out and creating the correct schema.

Post Reply