Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a navigation property for a Entity Framework Complex Type

I'm using VS2010 Beta 2, I have a Complex Type called Address with the following properties:

  • Street
  • City
  • CountryId

I have a Country Entity defined in my Model, but I can't seem to find a way to add a reference (Navigation Property) from the CountryId property of my Complex Type to the Id property of my Country entity.

I'm I going about this the wrong way or is this something that I can't do with the designer??...

Another option I have is just creating an Address entity, but it just doesn't sound right to me.

like image 542
Hector Minaya Avatar asked Oct 23 '09 16:10

Hector Minaya


2 Answers

No, you can't do this, because it goes against the idea of what complex types and navigation properties are. Complex types have value semantics, rather than reference identity. Navigation properties are first-class entities and have reference semantics. Therefore, they cannot be part of a complex type. As it seems you've discovered, the solution is to partition the parts with value semantics into the complex type, and add it to an entity containing the navigation properties you require.

like image 85
Craig Stuntz Avatar answered Oct 18 '22 00:10

Craig Stuntz


Ok,

It seems like the current version of the Entity Framework doesn't support a Navigation Property or foreign key within a Complex Type.

The other sensible option is to create a separate Address Entity, which will have all of the foreign keys it needs, and then create Navigation Properties within all my other entities.

like image 2
Hector Minaya Avatar answered Oct 18 '22 01:10

Hector Minaya