Tracking active document in Visual Studio 2017

I used to have active work item tracking turned on in my Solution Explorer, but that can get annoying, so I turned it off, but then I found I spend too much time searching for my active document. I figured there must be an easier way. Sure enough, Visual Studio has got me covered on this…

So, active item tracking is set in the configuration settings:

ActiveItem1

As you see, I have turned it off.

Now when you have an active document open in the editor, the Solution Explorer will start a bi-directional arrow icon:

ActiveItem2

Press it to have the solution explorer track down the active document in the solution explorer. The feature is called ‘Sync with Active Document’. Also accesible via keyboard shortcut: CTRL+[, S

 

 

Database-Update to recreate database on Azure

I’ve been working on an Entity Framework Code First solution using an Azure database. The migrations work like a charm, but often I’d like to recreate the database from scratch and re-seed from scratch. DropAndRecreate does not work due to permission issues, so the next best thing is to drop all tables in the database and use Database-Update from the Package Manager Console to recreate the database.

I use the following scripts to drop every constraint and then every table:


while(exists(select 1 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE='FOREIGN KEY' and CONSTRAINT_SCHEMA != 'sys'))
begin
declare @sql nvarchar(2000)
SELECT TOP 1 @sql=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']')
FROM information_schema.table_constraints
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
exec (@sql)
PRINT @sql
end


while(exists(select 1 from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA != 'sys'))
begin
declare @sql nvarchar(2000)
SELECT TOP 1 @sql=('DROP TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA != 'sys'
exec (@sql)
PRINT @sql
end

After which I use the Package Command Console to run:


Update-Database -ConnectionStringName AzureDevTest

Since the migration history table is missing it will run all the migrations.

Reset password via RDP session

The feature is ancient and you would think I would always remember, but just as a reminder to myself and to help those that don’t know:

When you’re logged into a remote machine using a remote desktop session and you want to change your password on the remote machine, use CTRL-ALT-END to see the change password option ( so END instead of DEL ).

rdp-password

Get the day of the week, in the right language

I’m working on an app that at some point will need to work in multiple languages and needed to display the day of the week to the user. I started with:

string label = DateTime.Today.DayOfWeek.ToString()

But that give me the name of the enumeration, which is of course in English. In order to get the name based on the current language setting I needed the following code:


var culture = System.Globalization.CultureInfo.CurrentCulture;
string label = culture.DateTimeFormat.GetDayName(DateTime.Today.DayOfWeek);

Happy coding!

Three issues (with solutions) when upgrading to Visual Studio 2017

In the last 2 weeks I’ve upgraded two (large) C# / ASP.NET solutions from Visual Studio 2015 to Visual Studio 2017. Good news? By and large things ‘just’ work. But there were three things that required attention:

  1. Issue: My project used PowerShell projects from “PowerShell Tools for Visual Studio 2015“. This VS2015 extension did not automatically get activated for VS2017.
    Solution:

    1. Download and reinstall the extension. The installer recognized that VS2017 was now installed and enabled the extension for VS2017.
  2. Issue: The projects were both using the Microsoft Unit Test framework and the unit test runner would run without issue in VS2017, but Live Unit Testing would not work.
    Solution:

    1. In the Unit Test project(s) remove the reference to: Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
    2. Use nuget to add MSTest.TestAdapter and MS.TestFramework to the unit test project:
      nuget-test
  3. Issue: My Team Services CI build failed after I started using C#7 syntax in my code.
    Solution: Install Visual Studio 2017 on the build server and change the build definition to use Visual Studio 2017 for building the solutions.
    Alas, this will not (yet) work if you’re using a Hosted Agent. So be sure to be using a build agent that runs on a machine where Visual Studio 2017 is installed.

Useful to know:

  • For a short time some team members were working with VS2015 and some with VS2017. No problem as long as you’re not using C#7 syntax just yet 🙂
  • Play around with including/excluding tests from Live Unit Testing, for larger solutions the performance hit of having all the tests being run all the time seems prohibitive.