Thursday, April 22, 2010

"Only models that include foreign key information are supported." error in Entity Framework 4

This is an error I encountered in EF4 within Visual Studio 2010, when I use the "Model First" approach and created a database from a theoretical model. Ironically it will not compile after that because of the error. Microsoft still has a lot of work to do here.

Solution:
1. Create the property within the entity for the FK using the same name.
2. In mapping details, map the new property to the underlying table field.
3. Select the original association and click the Referential Constraint and choose the right principal and dependent.
4. Now select the association again, you will see a message akin to "Mapping not allowed for exposed foreign key... Delete the mapping...".
5. Click on Delete the mapping link.
6. Recompile.
Viola! World peace...