Entity Framework nvarchar Case Sensitivity on Foreign key

前端 未结 2 1737
迷失自我
迷失自我 2021-01-05 04:20

I have fairly simple table structure as below and issue sounds strange to me. Though I have chosen to work around it but would like to take experts opinion.

I have t

2条回答
  •  遥遥无期
    2021-01-05 04:45

    @Ladislav has described the issue perfectly well but there is another approach that some people might be able to use. Individual columns in your tables can be made case sensitive so they behave in the same way as .Net;

    Users
    UserName nvarchar(250) collate Latin1_General_CS_AS Primary Key
    FirstName nvarchar(50)
    LastName  nvarchar(50)
    
    Registrations
    Id BigInt PrimaryKey
    User nvarchar(250) collate Latin1_General_CS_AS - Foreign to Users Table
    Date - DateTime
    

    Note the collate added to the column definitions. Now (assuming you have a foreign key defined) the database will enforce case consistency just for the UserName between both tables. You might need to prepare your data beforehand by setting all to upper/lower, and will probably need to drop and recreate indexes, constraints etc if using alter table.

    This is much lower impact than trying to change collation at the database level but still requires some thought and testing because depending on what other collations are in effect you might run into collation conflicts that need to be handled.

    There are many collations available so one of the other case sensitive options might be more suitable depending on your circumstances.

提交回复
热议问题