问题
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