2013-12-09 49 views
0
public MyEntities() 
     : base("name=MyEntities") 
    { 
     /* 
     * If DbContext.Configuration.ProxyCreationEnabled is set to false, 
     * DbContext will not load child objects for some parent object unless Include method is called on parent object.    
     * Reference: https://stackoverflow.com/questions/4596371/what-are-the-downsides-to-turning-off-proxycreationenabled-for-ctp5-of-ef-code-f 
     */ 

     this.Configuration.ProxyCreationEnabled = false; 
    } 

我修改了MyDataModel.Context.cs文件。我提到this Link實體框架修改Context.cs文件更改不會持續

問題我每次更改edmx文件(刪除表或從數據庫添加新表)時,都會重新構建該文件。所以我做的這個改變就會丟失。

我該如何保留這個改變?有沒有這樣的配置設置?

回答

2

您不應該修改任何生成的文件。改爲修改T4模板。在解決方案資源管理器中打開EDMX-Model的樹,在這裏找到名爲* .Context.tt的文件,將其打開並修改創建ctor的部分。在我的情況下,這是在模板的第60行。

保存tt文件後,它會自動運行並創建您想要的代碼。每次修改和保存EDMX模型時,這些更改都會恢復。

順便說一句:你可能不會覆蓋部分類中的現有ctor。但是通常,部分類文件也是擴展具有更多功能的生成對象的好方法。