Usage of wrong compiler during SQL Server Database Project building
Asked Answered
E

3

3

I have an issue with compilation of SSDT SQL Server Database Project by using Visual Studio 2015. I want to use C# 6 features inside my database project, but it seems like it is unsupported. For example, I have added the next class in my db project:

namespace Database1
{
    class ClassFile1
    {
        public string Str { get; } = string.Empty;
    }
}

I have tried to compile this, but I got the error:

CS1519: Invalid token '=' in class, struct, or interface member declaration

I have found out that the reason of this error is wrong version of compiler that used by VS 2015. The next compilation line is generated by VS:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702,2008 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation /preferreduilang:en-US /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\mscorlib.dll" /debug+ /debug:full /optimize- /out:obj\Debug\Database2.dll /subsystemversion:6.00 /target:library /warnaserror- /utf8output ClassFile1.cs 

As you can see, C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe is used and it is wrong.

I have tried compile db project from Developer Command Prompt for VS 2015 and this was done successfully, because inside this prompt csc.exe is Roslyn compiler (C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe) that support C# 6 features. You can check question How to run Roslyn instead csc.exe from command line?

I tried to compile the project by using MSBuild 14.0 and it was successfully done too.

The question is: how can I change/override version of compiler that my VS used for compilation of SSDT DB project from old C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe to new Roslyn compiler (C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe)?

Edacity answered 8/10, 2015 at 9:24 Comment(0)
I
1

You will need to target the version of clr that is used by the version of SQL that you will be deploying to:

SQL 2005-2008 R2 = CLR 2

SQL 2012 = CLR 4

You can't just run any version of the clr that is on the machine I am afraid.

ed

Impecunious answered 8/10, 2015 at 11:20 Comment(3)
I not got it. My project settings use SQL server 2014 target platform. And I work with SQL Server 2014. Why then if I use csc.exe from MsBuild it all build successfully and deployed successfully too? All I want to do is change compiler which is used by VS 2015 for buillding sql projects.Edacity
It isn't supported you have to use clr 4Impecunious
Ok, what properties should I change to use clr 4?Edacity
G
3

I was trying to do this as well and faced the same problem. My workaround is to create a separate regular class library project. I moved the C# code from my Database project into the new library (which will be compiled using C# 6/Roslyn by VS 2015). And then reference this class library from my Database project.

Just remember to set the properties of the reference to the class library: Model Aware=true and Generate Sql Script=true. I haven't tested this thoroughly, but I was able to deploy it from my Database project by using the publish command, and call the function inside the class library.

Gruff answered 23/5, 2016 at 7:18 Comment(0)
V
3

Good news!
Visual Studio 2017 will use the current C# compiler for SSDT Database Projects, so all the features of
C# 6 will finally work in SQL Server assemblies.

The path of the used compiler is:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\csc.exe

Because all of the C# 6 features are just pure compiler features you can now even use them if you are only targeting .NET 3.5 and SQL Server 2008.

In Visual Studio 2017 RC, I was also able to use some C# 7 features like expression bodied constructors, the new out variables, ref returns, the enhanced switch/is pattern matching and the improved binary/hex literals. But I was unable to use local functions or anything related to tuples like the new tuple return types, tuple literals or the new deconstruction declaration.
Maybe this behavior will change in the final release of Visual Studio 2017.

Vaniavanilla answered 29/11, 2016 at 11:30 Comment(0)
I
1

You will need to target the version of clr that is used by the version of SQL that you will be deploying to:

SQL 2005-2008 R2 = CLR 2

SQL 2012 = CLR 4

You can't just run any version of the clr that is on the machine I am afraid.

ed

Impecunious answered 8/10, 2015 at 11:20 Comment(3)
I not got it. My project settings use SQL server 2014 target platform. And I work with SQL Server 2014. Why then if I use csc.exe from MsBuild it all build successfully and deployed successfully too? All I want to do is change compiler which is used by VS 2015 for buillding sql projects.Edacity
It isn't supported you have to use clr 4Impecunious
Ok, what properties should I change to use clr 4?Edacity

© 2022 - 2024 — McMap. All rights reserved.