Naor
Naor

Reputation: 24063

remove foreign key property cause an exception

I don't want to use foreign key association to CompanyType (member that will hold the foreign key id) but prefer to use navigation property. So I removed the CompanyTypeId.

I get this exception that relates the relationship between entity Company and CompanyType:

Error 5: The element 'Principal' in namespace 'http://schemas.microsoft.com/ado/2008/09/edm' has incomplete content. List of possible elements expected: 'PropertyRef' in namespace 'http://schemas.microsoft.com/ado/2008/09/edm'.

enter image description here

How can I remove those id's from the POCOs without getting the exception?

Upvotes: 6

Views: 6148

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

This is the difference between Foreign key association and Independent association. Both associations use navigation properties but only Foreign key association uses FK property as well. You can either remove them globally as @Robbie mentioned or you can change the type manually for selected relation.

  • Select the relation in entity framework designer
  • In properties remove Referential constraints
  • Go to Mapping window and map the relation

Here is the screen shot from one of my testing application with one-to-many relation between Order and OrderLine entities:

enter image description here

As you can see there is no OrderId in the OrderLine entity and referential constraints of the relation are empty. Also mapping of the relation is specified.

BUT you can't never remove Id from CompanyType. Ids (PKs) are mandatory. You can only change its accessibility in its properties.

Upvotes: 8

Robbie Tapping
Robbie Tapping

Reputation: 2556

When you imported in your Model from your DB you are asked if you want to:

"Include Foreign key columns in the model" you need to switch this off.

enter image description here

Upvotes: 4

Related Questions