Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing navigation properties from POCO-classes in Entity Framwork

I'm using generated POCO classes and Entity Framework.

In order to make the code less complex I'm trying to remove all navigation properties from the code while still keeping the Foreign Key constraints in the database (navigation properties do more harm than good for us).

If I remove them manually from the POCO-classes I get the following error

The entity type UserEntity is not part of the model for the current context

If I try to remove them from the .edmx-file I get the following error:

Error 3 Error 3015: Problem in mapping fragments starting at lines 479, 562:Foreign key constraint 'fk_StorageContracts_User1' from table StorageContract (OwnerUserID) to table User (ID):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.

Is there any way to remove navigation properties from POCO-classes without removing the corresponding FK?

like image 753
Yrlec Avatar asked Nov 14 '22 06:11

Yrlec


1 Answers

I know this is old, but, since there is still no answer, I thought I'd give it a try:

I am still working in EF 4.0, but, following the example to which you referred, you have an xxxModel.tt. If you are willing to tweak that, you can find where it generates the Navigation Properties and change them to be simple auto-properties. I had a similar project where I generated them like this:

public List<NavDataX> NavDataXs
{
    get; set;
}

Now, they are still there, but they are null until you explicitly set them. Doing it this way, I did not mess with the EDMX and did not encounter the two errors you mentioned.

like image 107
Kelly Cline Avatar answered Dec 10 '22 05:12

Kelly Cline