简体   繁体   中英

Programmatically removing an Entity Framework navigation property

I need to remove some navigation properties from an entity, it would be useful if I could do this in code to save it having to constantly be done in the designer.

I tried adding an interface to a partial class of the entity that peppered the navigation properties with [NotMapped] this builds OK but still brings back the properties populated in the entity.

Is there another way to do this? EF 4.1, c#, VS 2010

  • In the designer

Just delete the navigation property in the designer (select it then simply click "del" on your keyboard), it won't remove the association between your entities, but there won't be any navigation property anymore.

  • By editing the code generation template

If EF added a T4 template in your solution that generates the entities from the Entity Model, edit the T4 template to remove the navigation property code generation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM