Entity Framework 6 Code First case insensitive string association

c# entity-framework entity-framework-6


I'm migrating an LINQ to SQL project to EF 6.1.3 Code First. The problem I'm facing is:

I have 2 tables, Incidents with debtorNo column (varchar(20) not null) and Debtors With a No column (varchar(20) not null) which is the primary key. The schema cannot change because these tables are populated by an external system on which I have no control. There is no foreign key between the tables in the db as the tables are populated asynchronously (again no control over that). The db collation is case insensitive.

In LINQ to SQL I declared an association between the tables for navigation. In EF I declared the same association with the following code in the DbContext's OnModelCreating method:

            .HasRequired(e => e.Debtor)
            .WithMany(e => e.Incidents)
            .HasForeignKey(e => e.DebtorNo);

While LINQ to SQL association was case insensitive the EF association is not. After googling the problem I found 2 solutions:

  1. Create and use views that convert the strings to upper or lower case.
  2. Manipulate the data

These solutions are perfectly working (I'll probably use the second one) but were dated a couple of years ago and referred to EF 4.1 Code First.

So my question is is there a way to declare a case insensitive string association in the latest version of EF Code First?

4/17/2015 11:57:23 AM

Accepted Answer

We are using EF 6.1 and, as far as I can determine, there is no way to do this. I think you will still need to use one of the methods you listed.

FWIW we are using views to do conversions at the database level (strings to dates for example)

4/17/2015 11:58:34 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow