CodeFirst IsFixedLength() issue

Dec 1, 2012 at 12:09 PM

 I'm having an issue I'm trying to solve.In CodeFirst entity configuration,


I'have the following configuration, in order to represent a MySql CHAR field: 

HasKey(p => p.Id);Property(m => m.Id).IsFixedLength().HasMaxLength(13);

When using this configuration, the objects are inserted, but the query do not find them:

Assert.That(repository.Members.Count(),Is.EqualTo(1)); (OK)

Assert.IsNotNull(repository.Members.FirstOrDefault(l => l.Id == id)) (FAILS)

Everything goes fine if I change "IsFixedLength" for "IsVariableLength"

Can you point me in the right direction to try to solve it?

Dec 1, 2012 at 8:56 PM
Edited Dec 1, 2012 at 9:04 PM

The field has fixed length, so if you put a shorter value in the field it will be expanded with white spaces.

For example, if you set the Id field as "0123456789", it will be stored as "0123456789   ". This means you should use this code:

Assert.IsNotNull(repository.Members.FirstOrDefault(l => l.Id == "0123456789   "))

However, I checked SQL server and it seems it works as you wrote. The char and varchar values seem to be right trimmed. 

How to overcome this issue? My advice is to create an expression rewriter and add it to the compiler of the in-memory engine. It should search for string comparisons and adds RTrim operations to the appropriate operands. Are you familiar with the .NET expression trees?
Dec 1, 2012 at 11:12 PM

I have the same tests runing a mysql database, with not problems. So it's clear that NMemory behaves differently than mysql with CHAR fields.

Since I'm not familiar with .NET expression tress, I'll try to adapt my own code meanwhile!

Thanks very much!