문제

Thanks for looking.

I have a small program that generates a new .NET solution based on some project information and a database connection.

As part of this process, I automatically generate the ADO.NET entity data model/.edmx file using edmgen2.

This code works fine and generates the .edmx, etc which is added to the project's folder in Windows.

The Problem

When I open the generated solution and subsequently the DAL project which contains the .edmx, I must then show excluded files, right-click, and then include the .edmx manually.

This isn't a huge deal, but it would be nicer if I could call come code after the .edmx is generated that would automatically include it in the VS Project.

Is this possible?

ADDING TO THE ANSWERS BELOW:

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 msbuild namespace. Here is what worked for me:

var csproj = XDocument.Load(SolutionFolder + @"\SomeProject.csproj");
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
var itemGroups = csproj.Descendants(msbuild + "ItemGroup");
var itemGroup = itemGroups.FirstOrDefault(x => x.Descendants(msbuild + "Compile").Any());
var item = new XElement(msbuild + "Compile");
item.SetAttributeValue("Include", "ApplicationEntityModel.edmx");
itemGroup.Add(item);
csproj.Save(SolutionFolder + @"\SomeProject.csproj");
도움이 되었습니까?

해결책

Not tested: You can edit your *.csproj file and add your new file to <ItemGroup> section. It is an xml file that can be edited programitically.

  <ItemGroup>
    <Compile Include="fileName" />
  </ItemGroup>

다른 팁

The C# project file *.csproj is actually a XML file, so after your tool finishes generating the solution, you can open the desired project file as XML (XDocument, XmlDocument, etc.) and add to it Item element which will point to the generated .edmx file.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top