SaveAll throws "SqlException: Must declare the scalar variable "@0"."

Hi ServiceStack,
I’m using version 4.5.14 (I’ve the same problem with 4.5.12, I’ve non tried with older versions). I’ve the following code

void Main()
	var connectionString = "SERVER=...; DATABASE=...; USER ID=...; PASSWORD=...;";
	var dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider);
	using (var db = dbFactory.Open())
		// This doesn't work: throws SqlException (Must declare the scalar variable "@0").
			new UserRole { Name = "Admin" },
			new UserRole { Name = "Reader" },
			new UserRole { Name = "Writer" },

		// This correctly works.
//		db.Save(new UserRole { Name = "Admin" });
//		db.Save(new UserRole { Name = "Reader" });
//		db.Save(new UserRole { Name = "Writer" });

// ...

class UserRole
	public string Name { get; set; }

If I use the Save method all works fine instead, if I use SaveAll method I get the reported exception.


I’ve added a test showing this works as expected in SQL Server. I’ve also tested with different SQL Server dialects + Sqlite, etc. If you can add a stand-alone Console App or test that fails I can investigate further.

Your test doesn’t create the table, so you might be using a definition of a UserRole table created by a different model.

you can download an example

The first time I run the code, all works as expected.
The second time I get the exception

SqlException: Must declare the scalar variable ”@0".

If I use Save instead of SaveAll, all works fine.


Thanks for the repro, this issue should be resolved from this commit. This change is now available from v4.5.15 that’s now available on MyGet.

Thank you for fixing the issue