4

I have a SQL Server data source which cannot be modify (I'm not the dbo). I use Entity Framework 6 model-first to do my querying. Everything is fine except that I must select costumer names and there's only two character discrepancy.

Datasource            Wanted
=============================
   ¤                    Ñ
   ¥                    ñ
  MU¤OZ               MUÑOZ
 Zu¥iga               Zuñiga

So.. there's a transparent way to do so? Obviously... you can make a method to replace and just call when you query the costumers table. But what I want is make a solution that works with all the queries that already create and will write too.

I read about implementing

System.Data.Entity.Infrastructure.Interception.IDbCommandInterceptor

but not sure if its the right way.

An exaple of a query and its result;

var aCostumer = db.costumers.Where(n=>n.idCostumer==someId).Select(n=>new{n.idCostumer,n.firstName,n.lastName}).FirstOrDefault();

Result

idCostumer    firstName    lastName
===================================
   1             DAVID       MU¤OZ

But I want that query result were:

idCostumer    firstName    lastName
===================================
   1             DAVID       MUÑOZ

Can someone point me the right direction? Thanks in advance

Robert K
  • 130
  • 3
  • 16

1 Answers1

0

You can add a property to your Customer model class that does the replacement and doesn't get mapped or serialized.

This would just mean replacing all references to "Name" with "CorrectName".

Code-first:

public class Customer
{
    // ...

    public string Name { get; set; }

    [NotMapped]
    public string CorrectName
    {
        get { return Name.Replace('¤', 'Ñ').Replace('¥', 'ñ'); }
        set { Name = value.Replace('Ñ', '¤').Replace('ñ', '¥'); }
    }

    // ...
}

Model-first:

// In a new file, so your changes don't get overwritten:
public partial class Customer
{
    public string CorrectName
    {
        get { return Name.Replace('¤', 'Ñ').Replace('¥', 'ñ'); }
        set { Name = value.Replace('Ñ', '¤').Replace('ñ', '¥'); }
    }
}
c b
  • 148
  • 7
  • Ok let me try, just to clarify... what if you update your model? NotMapped tag prevent the property deletion? – Robert K Aug 12 '15 at 00:01
  • Since it's model-first, you can add the "CorrectName" property in a separate file using partial classes ("public partial class Customer { ... }"). NotMapped may only be needed for code-first - I'm not sure of the consequences for model-first, sorry. – c b Aug 12 '15 at 00:24