I have a database that is supposed to store data in any language, there is going to be a column that tells me which locale it is, so i can’t rely on database collation and will have to specify collation at runtime in queries.
I also have the problem that i want to use EF for dataaccess, as we know using EF one cannot specify collation at runtime. I am thinking about creating a sql function that takes collation as argument and apply that function in all of the Linq Queries.
but this fails
CREATE FUNCTION fn_Compare
(
@TextValue nvarchar(max),
@Culture varchar(10)
)
RETURNS nvarchar(max)
AS
BEGIN
RETURN @TextValue COLLATE @Culture
END
GO
does anyone know if this can be done ?
You cannot do this. The collation returned by the function needs to be consistent across all the return values. For instance, the following generates an error:
The error is due to a collation conflict.
What you can do is use:
Or, alternatively, create a new working database with the collation you want.