tags:

views:

80

answers:

1

I have a Customer class that contains a property, MyProperty, which is of a custom type MyCustomType. I want to persist the property value in the database as text. In the designer I've set the Type to 'MyType' and the Server Data Type to 'varchar(10)'. When I build the project I get the following error:

DBML1005: Mapping between DbType 'varchar(10)' and Type 'MyType' in 
Column 'MyProperty' of Type 'Customer' is not supported.

Now that make sense, as Linq to Sql has no way of knowing how to convert my custom type. So I'm assuming I have to implement some kind of Parse(string) and ToString() methods on MyCustomType however I can't find any documentation on this.

So, how do I map custom types in Linq to Sql?

+1  A: 

A class to a varchar? I'm not aware of any functionality that supports this in LINQ-to-SQL. Your best bet may be a simple property (it can be private if you need):

[Column(Name="ColumnName", DbType="varchar(10) NULL", CanBeNull=true)]
private string MyPropertyString {
    get { /* serialize MyProperty yourself */ }
    set { /* deserialize MyProperty yourself */ }
}
public MyCustomType MyProperty {get;set;}

(i.e. read from or update your actual MyProperty property)

I also expect that you'd have to leave this off the designer, and add it yourself (and the MyProperty property) in a partial class.

Marc Gravell
A good work-around, it achieves what I needed.
David G