Is it possible to define a SQL case sensitive primary key in entity framework code first? I know that by default SQL is case insensitive when it comes to strings, just to be clear I don't want to change the entire DB definition to case sensitive, just one table column (primary key).
I'm getting data from an external API which sends the data with case sensitive primary keys ('a' and 'A' are different records), I know I can modify them and save them differently in my DB, but this will also require me to be consistent about it everywhere in my code. That's defiantly possible, but I would rather just avoid it.
Ideally I was hoping to find a way to define my primary key as case sensitive via the entity framework and not using SQL queries.
I would appreciate any suggestions, or better yet a simple way to do it.
Update
OK, so I've pretty much lost hope for this being possible, and now when I try to use the other approach which is:
public ovveride Up()
{
// drops the existing primary key named PK_dbo.Urls
Sql("ALTER TABLE dbo.Urls DROP CONSTRAINT [PK_dbo.Urls]");
// change the collation
Sql("ALTER TABLE dbo.Urls ALTER COLUMN Url VARCHAR(10) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL");
// re-add the primary key to the Url column
Sql("ALTER TABLE dbo.Urls ADD CONSTRAINT [PK_dbo.ShortUrls] PRIMARY KEY (Url)");
}
I can't really go through with it since I have 4 different constraints on this field (primary key + 3 others).
I don't want to rewrite the constraint code myself. So can I use fluent API to do the case-sensitive change?
This is the auto-generated fluent API code:
CreateTable(
"dbo.Shapes",
c => new
{
TabID = c.Int(nullable: false),
Lbl = c.String(nullable: false, maxLength: 128),
wasRemoved = c.Boolean(nullable: false),
})
.PrimaryKey(t => new { t.TabID, t.Lbl })
.ForeignKey("dbo.Tabs", t => t.TabID, cascadeDelete: true)
.Index(t => t.TabID);
I would really appreciate a solution, since I've been stuck on this for a while now, and I'm looking for the best of worsts right now.
Update 2
I also found a 2nd way of doing this, it's fully automatic, well after you define a lot of other things, anyway it uses a custom data annotation. see my answer below for more info.