Entity Framework Code First - Ignore complete entity

我是研究僧i 提交于 2019-12-10 20:11:45

问题


Is there an easy solution for the following situation: I've got an Entity Dossier which is shared in 2 DbContexts. Dossier has 2 navigation properties, DossierType and DossierAttachment

Dossier

Dossier --> DossierType (Navigation Property)

Dossier --> DossierAttachment (Navigation Property)

I want to exclude the DossierType Entity in DbContext1. I do not want it to be savable to database, navigation properties should not be set.

I was expecting that calling

modelBuilder.Ignore<DossierType>(); 

in the DbContext1.OnModelCreating would be sufficient. It should make sure that my navigation property DossierType was not 'taken into context'. It however gives following exception:

The navigation property 'DossierType' is not a declared property on type 'Dossier'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

This exception will keep coming unless I call the Ignore on the navigation property explicitly, which I do not want. (My reallife scenario has a lot more navigation properties, I do not want to iterate these)

modelBuilder.Entity<Dossier>().Ignore(x => x.DossierType);

Is there any easy way to ignore specific entity types in an easy straight forward manner? Or should I really go for a reflection way: declaring a list of types which I don't want and ignore them using reflection?

来源:https://stackoverflow.com/questions/27543396/entity-framework-code-first-ignore-complete-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!