Search code examples
c#entity-frameworkentity-framework-ctp5

Entity Framework Code First CTP5 : How to define non primitive types


I'm testing the CTP5 for entity framwork code first, and i've run into this problem I've got a class that has a property of type Uri (System.Uri), but it looks like it's unable to automatically identify how to store that, so i get an error like

Problem in mapping fragments starting at line 23:No mapping specified for properties WebPage.Uri in Set WebPage

How can i tell the model to map the Uri to a varchar, for example, with the url of the uri??


Solution

  • The actual POCO model has to bind to primitive types. You can use a complex type binding such as:

    [ComplexType()]
    public class UriHelper
    {
        public string StringRepresentation {get;set;}
        public Uri ActualUri()
        {
            return new Uri(StringRepresentation);
        }
    }
    

    And in your actual object reference this complex type as the Uri reference if you absolutely need to. Your mapping would then reference the property for the actual value as a String. The final option is to create a custom mapping from URI to string and vice versa for the EF engine to use. However, I would not advise this. The actual database property is of type varchar or nvarchar, not URI. Thus EF doesn't know what a URI is.