Categories
Development

Subsonic 3 – ActiveRecord – T4 tweak

Quick reminder : I’m using Subsonic 3 ActiveRecord on a MSSQL database.

One of the nice things with Subsonic 3 is to be able to generate your classes from the database fast and easy.

But sometimes when you are many working on the same project or when you work on different machines, it happens that your database doesnt have exactly the same name  as the original and the T4 files provided will not generate the parameters for your stored procedure.

A quick fix is to edit the method GetSPParams in SQLServer.ttinclude

Replace this line :

string[] restrictions = new string[4] { DatabaseName, null, spName, null };

With these

SqlConnectionStringBuilder connStringBuilder = new SqlConnectionStringBuilder(ConnectionString);
string[] restrictions = new string[4] { connStringBuilder.InitialCatalog, null, spName, null };

And then simply run your .tt files again.

Download the zip file containing the modified SQLServer.ttinclude

If you have improvements that you have made, feel free to write about them in the comment section.

Categories
Development

Subsonic 3 – ActiveRecord – Bug DateTime with default value

Tonight I just had the weirdest bug using Subsonic 3 and I just solved it, and if it can save you some time here is the solution for my case.

If you get this error :

System.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0 – No process is on the other end of the pipe.) …

only on the saving operation for one particular table in your database. If the debugger in you find it strange, you’re not alone.

After searching, I found out that I forgot to initialize a datetime field (property) on one of my object.

When a datetime is not set in .Net, it sets the date to a minimum value. This is where the problem starts. That value is not within the acceptable range for a datetime type in Microsoft SQL Server (MSSQL).

Instead of giving a clear exception, the exception thrown is a transport-level error.

Hope it helps.

Good night.