If I write a string to a SQL Server table using Entity Framework (migrated an existing database to EF), then measure the string length in SQL Server Management Studio, my string is way too long. It appears that the different collation types are adding a bunch of white space at the end of the string.
Do I have to set the collation type for EF explicitly? If so, is there a better method than this?
Got the default collation:
SELECT SERVERPROPERTY('collation') SQLServerCollation
result:
SQL_Latin1_General_CP1_CI_AS
I measured the string like this (returns 51 characters, when the string is "Bill"):
SELECT
LEN(CONCAT(first_name, '!' COLLATE SQL_Latin1_General_CP1_CI_AS))
FROM
[GreenCardSite].[dbo].[AppUser]
WHERE
user_id = 16840
Note that the ! is added to the end of the string because by default SQL Server does not measure white space when calculating length. Further, when I ran this without specifying the collation I got an error:
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_100_CI_AS" in the concat operation.
AppUser
EF class:
[Table("AppUser")]
public partial class AppUser
{
[Key]
public int user_id { get; set; }
[StringLength(50)]
public string first_name { get; set; }
[StringLength(50)]
public string last_name { get; set; }
.....