ObjectContext public in debug mode, internal in release mode

折月煮酒 提交于 2020-02-06 02:51:34

问题


Is there an easy way to make an ObjectContext public in debug mode and internal in release mode?

With Linqpad it is very convenient to connect to an ObjectContext for quick testing and querying, so for development purposes I want it to be public. But I don't want to think of the consequences when the same convenience is deployed to some smart customer.


回答1:


As mentioned in the comment, this may not be of any practical use, but:

#if DEBUG
public
#endif
class YourContext : ObjectContext
{
    ...
}

When dealing with a generated ObjectContext from a .edmx file, you'll need to customize how C# files are generated. The default is not customizable, but the designer has an option "Add Code Generation Item". If you use this, you'll get several options. I'm using "ADO.NET Self-Tracking Entity Generator", but the same way works for all of them. Choosing this adds two template files (Model.tt and Model.Context.tt) to your project, which you are free to modify as you see fit. For the modification you're asking about, you'll find <#=Accessibility.ForType(container)#> partial class in the Model.Context.tt file. You can update this so that it reads

#if DEBUG
<#=Accessibility.ForType(container)#>
#endif
partial class



回答2:


Preprocessor directive?

# if(DEBUG)
        public ObjectContext _context;
# else
        internal ObjectContext _context;
#endif


来源:https://stackoverflow.com/questions/9462116/objectcontext-public-in-debug-mode-internal-in-release-mode

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