title: Using C#, how do I "Include" a file into a project?
body: <p>Thanks for looking.</p>
  <p>I have a small program that generates a new .NET solution based on some project information and a database connection.</p>
  <p>As part of this process, I automatically generate the ADO.NET entity data model/<code>.edmx</code> file using <a href="http://archive.msdn.microsoft.com/EdmGen2" rel="nofollow noreferrer">edmgen2</a>.</p>
  <p>This code works fine and generates the .edmx, etc which is added to the project's folder in Windows.</p>
  <h1>The Problem</h1>
  <p>When I open the generated solution and subsequently the DAL project which contains the <code>.edmx</code>, I must then show excluded files, right-click, and then <code>include</code> the .edmx manually.</p>
  <p>This isn't a huge deal, but it would be nicer if I could call come code after the <code>.edmx</code> is generated that would automatically include it in the VS Project.</p>
  <p>Is this possible?</p>
  <h1>ADDING TO THE ANSWERS BELOW:</h1>
  <p>The answers below got me going but I did find that I couldn't operate on the .csproj xml in the usual ways that I alter other xml files.  You have to use the <code>msbuild</code> namespace.  Here is what worked for me:</p>
  <pre><code>var csproj = XDocument.Load(SolutionFolder + @&quot;\SomeProject.csproj&quot;);
  XNamespace msbuild = &quot;http://schemas.microsoft.com/developer/msbuild/2003&quot;;
  var itemGroups = csproj.Descendants(msbuild + &quot;ItemGroup&quot;);
  var itemGroup = itemGroups.FirstOrDefault(x =&gt; x.Descendants(msbuild + &quot;Compile&quot;).Any());
  var item = new XElement(msbuild + &quot;Compile&quot;);
  item.SetAttributeValue(&quot;Include&quot;, &quot;ApplicationEntityModel.edmx&quot;);
  itemGroup.Add(item);
  csproj.Save(SolutionFolder + @&quot;\SomeProject.csproj&quot;);
  </code></pre>