LINQ, SqlMetal, Nesting The Generated Code File
7 July 2008LINQ is really cool, SqlMetal has been my code generator of choice (for a book on LINQ, SqlMetal and all of the cool new features)… I like to keep my class file names the same as the class contained in the file. Since SqlMetal generates partial classes, and we don’t want to modify the generated file directly, well now we are stuck creating a new file name that doesn’t match our naming convention. (note: you could stick the generated code in a generated directory or something, which is what I had done in the past… UNTIL NOW
)
Enter the DependentUpon element tag in your project file. Real quick open a Windows Forms, or Web project. Notice how the webforms or windows forms classes have a *.designer.cs code file nested underneath the actual form. Not only does it help us keep the files grouped together, but it allows us to keep naming conventions and keeps the project solution clean… Now that you have your project open, unload it, then Edit the project file directly (so you can see the XML)… Scroll down to the Compile items collection… Ah Ha! do you see that? Its the DependentUpon element nested underneath the *.designer.cs files.
You can use this for your Linq DataProvider class library projects. Here’s the system that I use.
A Regenerate batch file (DbGen.cmd)
Modify your csproj file to use the DependentUpon directive on your generated code file.
<Compile Include=”website.cs” />
<Compile Include=”website.sqlmetal.cs”>
<DependentUpon>website.cs</DependentUpon>
</Compile>
All of your custom code goes into the base named code file (website.cs in my example)
No comments yet
Leave a Reply
You must be logged in to post a comment.
