Visual Studio 2012 Post Build Event – Run Batch – Validate Entity Framework Model ProviderManifestToken

Visual Studio 2013

June 3, 2015 | Posted in Visual Studio 2012, Windows

When entity framework models are refreshed from a SQL database, the ProviderManifestToken will change based on the version of SQL Server.

It was necessary to automate a process whereby every time the DAL built, I needed to verify that a refresh hadn’t inadvertently changed the ProviderManifestToken to an incorrect version.

To do this, I created a batch file (all edmx files must conform to SQL Server version 2005, and if they don’t, notify):

 

 

@echo off
@break off
@title Create folder with batch but only if it doesn't already exist - PROVIDER MANIFEST SEARCH
@color 0a
@cls

setlocal EnableDelayedExpansion

set shouldpause=0

if exist "C:\ProviderManifestTokens\" (
  rmdir "C:\ProviderManifestTokens\" /s /q

)

if not exist "C:\ProviderManifestTokens\" (
  mkdir "C:\ProviderManifestTokens\"  
)

set SOURCE_DIR=%cd%
set DEST_DIR=C:\ProviderManifestTokens
set FILENAMES_TO_COPY=*.edmx

for /R "%SOURCE_DIR%" %%F IN (%FILENAMES_TO_COPY%) do (
    if exist "%%F" (
    	set FILE_DIR=%%~dpF
    	set FILE_INTERMEDIATE_DIR=!FILE_DIR:%SOURCE_DIR%=!
    	xcopy /I /Y "%%F" "%DEST_DIR%"
    )
)

if exist "C:\ProviderManifestTokens\" (

  chdir /d C:\ProviderManifestTokens\

  ren *.edmx *.txt

  for %%F in (.\*) do (
  find "ProviderManifestToken=""2005" %%F > nul
        if errorlevel 1 (		

		echo ----------------------------------------------------------
		echo Warning: File %%F contains incorrect ProviderManifestToken
		echo ----------------------------------------------------------
		set shouldpause=1
    )
  )
)

if not %shouldpause% == 1 (
	echo ----------------------------------------------------------
	echo Success: *.edmx files valid 
	echo ----------------------------------------------------------
)

exit

 

 

 

 

The script essentially scans directories and sub directories for all edmx files, it then copies them to a folder on the root, changes file extensions from *.edmx to *.txt (so that I could perform string lookups) and then notify where the ProviderManifestToken wasn’t what I wanted it to be.

The only other bit is to add the following in to your project Build Events:

cd $(ProjectDir)
call CheckProviderManifestTokens.bat

The above is a Post Build event and is set to run Always.

Check your Output window for the results of the batch file.

Update: I ended up leaving the batch in the DAL where it was, and putting the post build event in to the main application project, as that project is the last to compile, therefore developers will see the results at the end in the output window.

The post build event changed everso slightly:

cd $(ProjectDir)\DalFolder
call CheckProviderManifestTokens.bat

Read More →

Installing and Uninstalling Packages using NuGet Package Manager Console

Visual Studio 2013

July 15, 2014 | Posted in Visual Studio 2012

In Visual Studio under Tools, if you have NuGet installed you should see an option “NuGet Package Manager… Console”.

Here you can install and uninstall packages in the console by specifying the  -package, the -version and the -project and it takes care of the installation and references for you:

 

Install-Package EntityFramework -Version 5.0.0 -Project MyProject

Uninstsall-Package EntityFramework -Version 5.0.0 -Project MyProject

Read More →

VS2012 Schema compare buttons disabled

Visual Studio 2013

November 15, 2013 | Posted in Visual Studio 2012

There have been many trials and tribulations during the upgrade process from Visual Studio 2010 to Visual Studio 2012, not least the SQL Schema Compare functionality.

Having done the initial upgrade between the previous to the new version of Visual Studio, all .dbproj files were converted over to the new .sqlproj format. If you have written any SQL CLR projects in Visual Studio 2010 i.e. .csproj format, these too will get converted to .sqlproj files.

The first thing you will realize is that the references from your new .sqlproj file will throw syntax errors saying that your schema objects i.e. sprocs/functions etc can no longer resolve your CLR proj references. To get around this, go to properties on the reference too your CLR proj file, and change property “Model Aware” to True, this will allow you to compile again successfully.

The new way of doing a schema compare is to create a schema compare file. From with the VS IDE, go to SQL -> Schema Compare -> New Schema Compare. It’s important to note that the settings regarding which types to ignore are now held in this .scmp file. Once you’ve been through the settings and configured it to your liking, check this file in to TFS so that you can reuse it, otherwise you’ll end up having to go through these settings time and time again.

If you try and compare your new .sqlproj file to a SQL Server database, you’ll notice that the two “Update” and “Generate Script” buttons are disabled.

VS2012 Schema compare buttons disabled

Through a lot of digging, I eventually found that the reason for this is that under your .sqlproj settings, you need to set a “Default” value on any “SQLCMD Variables” you may have included (this is the name of the option on the left hand side). Having done this, the options should become available (note you may need to restart the solution).

sqlcmd_variables

The behaviour of these SQLCMD Variables seems to be that it copies the values to your .sqlproj.user file, which the schema compare relies on. If you do not set a default value, you won’t be able to do a compare and generate scripts. The value you provide in the “Default” value will be stored in the project file and can be checked in to source control, however the “Local” value is specific to your machine which gets stored in your .sqlproj.user file I mentioned previously (which is not under source control). If you leave the Local value blank, the Default value will be used.

If this helped solve your issue, leave a comment and let me know.

Read More →

Referencing CLR Projects in Visual Studio 2012

Visual Studio 2013

October 9, 2013 | Posted in Visual Studio 2012

When migrating your database projects from Visual Studio 2010 to Visual Studio 2012, you may run in to a problem. If you have a database project referencing a c# class library which is your CLR project, you’ll find the reference no longer resolves. Why?

Simply right-click the CLR reference in your database project, go to properties, and set “Model Aware” to true, and your issue will be resolved.

Read More →