Search code examples
c#.netentity-frameworkentity-framework-5dbset

Is there a way to make DbSet.AddOrUpdate case sensitive?


I have a data set (show below) where each record is unique on two fields when considering case senstivity -

var dataSet1 = new DataSet() { Country = "UK", City = "London", ... }
var dataSet1 = new DataSet() { Country = "UK", City = "LONDON", ... }

On executing DBSet.AddOrUpdate() as below -

database.DataSets.AddOrUpdate(ds => new
{
    ds.Country,
    ds.City,
}, dataList.ToArray());

database.SaveChanges();

I am getting System.InvalidOperationException: Sequence contains more than one element

The cause of this exception is pretty obvious as it will return two records when querying for City = 'London'

Question - Is there a way to make DbSet.AddOrUpdate case sensitive?

PS: The dataset is coming from external source so I cannot tweak it to persist. For instance, change City to UPPER CASE and ignore duplicates is not allowed.


Solution

  • You need to set your database to Case Sensitive. At the moment it's CI (Case Insensitive).