EF6 and ConvertValue

Viewed 851

In EF Core you can convert a value from one type to another in the following manner:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<MyClass>()
    .Property(x => x.MyProperty)
    .HasConversion(
        v => ...
        v => ...
    )
}

I know this feature is not supported in EF6 but what would be a workaround to accomplish the same thing? The problem in my case is that I have to convert a varchar field to a Guid and I can't change the type directly in the database to an uniqueidentifier unfortunately. Note: This will used together with odata.

Just doing this:

[EnableQuery]
public IHttpActionResult Get()
{
    return Ok(from p in db.Persons
        select new Person
        {
            Guid = Guid.Parse(p.GuidString),
            Name = p.Name
        });
}

would result in the error:

LINQ to Entities does not recognize the method 'System.Guid Parse(System.String)' method, and this method cannot be translated into a store expression.

0 Answers
Related