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())
	{
		db.CreateTableIfNotExists<UserRole>();
		
		// This doesn't work: throws SqlException (Must declare the scalar variable "@0").
		db.SaveAll(new[]
		{
			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.

Thanks.

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.

Hi,
you can download an example

https://ifinformatica-my.sharepoint.com/personal/d_mantovani_ifinformatica_it/_layouts/15/guestaccess.aspx?docid=09d512ce2588b47f1bdc5811b10b5c100&authkey=AWx8_suRPVJgT6NP2mfkKGU.

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.

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