Usage of wrong compiler during SQL Server Database Project building

点点圈 提交于 2019-12-20 02:32:20

问题


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)?


回答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




回答2:


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.




回答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.



来源:https://stackoverflow.com/questions/33011682/usage-of-wrong-compiler-during-sql-server-database-project-building

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!