Migrating to new VS2017 .csproj format with migrations - visual-studio-2017

I am converting my project files to this new and shiny VS 2017 project format. I start out by replacing the content with this
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>library</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
</Project>
Then I add Assemblies and Nuget packages slowly and it all compiles. But how do handle all my migrations? I have around 400. Do I need to add all these as an embedded resource?

You don't have to do anything.
The migrations are code files so they need to be compiled. According to the documentation here, all code files are included in the compilation by default.
And of course, If you try to add a migration to the project with the new format, you will see that nothing changes in the csproj file.

Well. There is actually something you need to do, to make commands like Update-Database or DbMigrator class work. At least that's what I needed to do for my EF6 project.
<PropertyGroup>
<EmbeddedResourceUseDependentUponConvention>true</EmbeddedResourceUseDependentUponConvention>
</PropertyGroup>
and then to make it work you need to upgrade your Entity Framework to version at least 6.3

Related

Nuget not installing in Azure DevOps build

I'm trying to stop archiving into code source repository anything that comes from a Nuget package. For most, since they only have dlls, it works.
My problem is that I have one Nuget with a Content folder in the nupkg. When building using VS2017, everything gets installed correctly but when doing the same thing using the build server (Azure DevOps Pipeline), I get
Error MSB3030: Could not copy the file "....." because it was not found.
The pipeline agent version is v.2.122.1 according to Capabilities.
NuGet Task logs version 0.2.31 using NuGet 3.3.0.212 with MsBuild 14.0.
I see the nupkg files in the packages folder but the Content folder items don't get copied.
Is there anything special I need to add to my csproj or any additional steps I should add to my build definition?
Is there anything special I need to add to my csproj or any additional steps I should add to my build definition?
You need change your csproj file to link the files from content package instead of add the files into the project. Since you are trying to stop archiving into code source repository anything that comes from a Nuget package, then add a nuget restore task to your build definition.
As test, I create a content package and add a copy event to copy file from the content package:
After installing that package, following content will be add to the project file .csproj and packages.config:
.csproj:
<ItemGroup>
<Content Include="resources\Test.txt" />
<Content Include="resources\Test2.txt" />
</ItemGroup>
Packages.config:
<packages>
<package id="TestContentPackage" version="1.0.0" targetFramework="net461" />
</packages>
Then we change the .csproj file to link the files from content package:
<ItemGroup>
<Content Include="..\Packages\TestContentPackage.1.0.0\content\resources\Test.txt" />
<Content Include="..\Packages\TestContentPackage.1.0.0\content\resources\Test2.txt" />
</ItemGroup>
After change, the files from content package are not added to the project, just link to the project:
To verify the copy command, I add following copy command in the build event:
if not exist $(ProjectDir)TestFolder mkdir $(ProjectDir)TestFolder
xcopy /y "$(SolutionDir)Packages\TestContentPackage.1.0.0\content\resources\Test2.txt" "$(ProjectDir)TestFolder"
All the things work fine on my local.
Then I build this project with Azure devops, you need add the nuget restore task, otherwise, Visual Studio/MSBuild could not find the files from the packages. And it also work fine.
Note: If you want to copy the files from the content folder, you need copy it from the packages folder.
Hope this helps.

C++ vcproj OutputDirectory macros

In vcproj file i have
<Configuration
Name="Release|Win32"
OutputDirectory=".\Release"
...
>
What is .\Release it is some kind of macros? In what settings is it indicated? How setup him?
That looks like a very old version of VC++, but you did not specify which. The (not so) new MSBuild project files have the extension vcxproj and have a different format:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>.\Release</OutDir>
</PropertyGroup>
Nevertheless, you make these changes from the Project properties. By default, the output directory is $(SolutionDir)$(Configuration)\ in which case the <OutDir> setting is missing. You can, however, explicitly set a different output directory.
I know this refers to the MSBuild project format but these are handled similarly in older versions that were using VSBuild.
.\Release is output directory name. Whenever you build your project compiler will create folder Release in the current directory and output will copy to that directory.

Enable Visual Studio's C++ Core Check analysis only on project files?

I am a big fan of the C++ Core Guidelines and I like to follow them in all projects I work on, so I enabled the following option in my project template in Visual Studio 2017:
This tool is great and helps me write better code, but I simply cannot figure out how to make it only analyze my files. Whenever my project has a dependency such as Boost or OpenCV, I will get plastered with a wall of warnings:
These dependencies are added through vcpkg, however, the same thing happens when adding them manually with C/C++ > General > Additional Include Directories.
Is there any way to only make these warnings apply to project files, and not all included files?
As mentioned in the comments, right after the following section in your .vcxproj near the end of the file:
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
The problem may be solved by adding the following after the section mentioned above:
<PropertyGroup Condition="'$(Language)'=='C++'">
<CAExcludePath>$(QTDIR)\include;.\GeneratedFiles;$(CAExcludePath)</CAExcludePath>
</PropertyGroup>
Furthermore, if you are using vcpkg, which was the case in my situation, you will need to add the following element to the CAExcludePath:
$(VcpkgRoot)include
This will ensure that all headers from any packages will not be analyzed.

How add existing file to Different Visual Studio Project Programmatically

I am newbie.
I want to create a Visual C++ project as programmatically.
I have tried EnvDTE objects but they are working on instance of current solution but I want to edit different project file of different solution that not opened anywhere.
How can I get instance of different project to edit it?
And I am using Visual Studio 2013 Express. Is there any limimation for it? Should I use VSPackage's?
Thanks.
In general I would recommend considering a build system like CMake or Scons. It allows to generate build scripts for arbitrary platform (as such it includes MSVS solution files).
However, you might simply edit project file with some script or program. Project files are plain xml files and it's easy to add external files.
<ItemGroup>
<ClCompile Include="some_file.cpp" />
</ItemGroup>

How does Visual Studio 2010 hosts MSBuild for C++ projects?

I have a solution with several C++ projects. For some of the projects I need some custom file copy, e.g. to copy some configuration files to the output directory or to copy the output files of one project to a specific folder after build.
In some cases I don't want or cannot add these files to the projects directly through the Visual Studio IDE. I created simple .targets files which I can reuse and add to the projects which need the file copying.
Here is a simple example .targets file for copying configuration files:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
CopyCustom
</BuildDependsOn>
</PropertyGroup>
<ItemGroup>
<CustomFiles Include="$(ProjectDir)Config\**\*.xml" />
</ItemGroup>
<PropertyGroup>
<DestCustFolder>$(OutDir)Config\</DestCustFolder>
</PropertyGroup>
<Target Name="CopyCustom"
Inputs="#(CustomFiles )"
Outputs="#(CustomFiles ->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')">
<Message Text="Copy custom files..." />
<Copy SourceFiles="#(CustomFiles )" DestinationFiles="#(CustomFiles->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')" SkipUnchangedFiles="true" />
</Target>
</Project>
Through the "Build Customization" dialog in Visual Studio I add it to the project so it will be included like this at the end of the project file:
<ImportGroup Label="ExtensionTargets">
<Import Project="..\Targets\CopyCustom.targets" />/
</ImportGroup>
This should enable incremental build of my custom target. If I just edit one of my custom files (and none of the C++ files) and build it form the console with
msbuild foo1.vcxproj
it will actually detect the changes and does an incremental build for my custom target. If no changes are made the target is skipped.
If I do however build inside Visual Studio it will not detect changes to the custom files and only and gives me the message that the project is up to data:
========== Build: 0 succeeded, 0 failed, 5 up-to-date, 0 skipped ==========
I would have to additionally change one of the C++ files to make it check all targets again and to the incremental build.
I was expecting that Visual Studio just executes MSBuild which will then do the up-to-date check on the projects, so it should be the same result as running MSBuild from the console. I was trying to get more information by setting the verbosity level to diagnostic but I just get the same line. It seems to me that MSBuild is not even executed for the project but Visual Studio itself determines that the project is up-to-date.
So I was wondering how Visual Studio actually determines when it should execute MSBuild for a project.
I asked basically the same question before on the MSDN forum but couldn't get a clear answer.
See this suggestion on Microsoft Connect.
Basically you need to set DisableFastUpToDateCheck property to true to disable the fast-up-to-date check.
Just add to your vcxproj or your targets file:
<PropertyGroup>
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>
I found an answer by looking into the book "Inside the Microsoft Build Engine, Second Edition".
Note: I also updated the same in my question in the MSDN forum but I will mainly duplicate the text here again for completeness.
On page 280 they actually saying that the IDE does a "fast up-to-date check" on the project-level. It only spawns a project build and does a more fine-grained check on the individual tasks if this rough project-level check fails.
When running MSBuild from the command line however there is always a fine-grained up-to-date check on the individual tools.
So the IDE only seems to do this fast check on the files which are added to the projects directly and set as one of the "Input File" types.
In my opinion this is not a good design. I would prefer that the IDE is only used to edit the MSBuild project files and then just invokes MSBuild to do the up-to-date check. This would make it much clearer.
I can understand that in a solution with a lot of projects it can make the up-to-date check much faster but there should be at least an option to disable the fast up-to-date check. I was checking the IDE if there is a way to disable this behavior but could not find anything.
The solution suggested here actually works and I am using it at the moment. But I added several custom targets for different kinds of custom files. If I add a new custom file I should never forget to set it to "Custom Build Tool" otherwise the incremental build for this file will not work.
I guess a solution would be to make a full build customization for my custom files and use specific file extensions for them so Visual Studio will automatically detect them when I add them to the project and sets the right Item Type.