views:

178

answers:

1

In .NET 3.5sp1,

I have a table in Sql Server that is defined somehow like this:

CREATE TABLE Employee(
 EmployeeId [int] IDENTITY(1,1) NOT NULL,
 UserName varchar(128) NOT NULL,
 [Name] nvarchar(200) NOT NULL,
 Address xml NULL,
    ...)

I am mapping this table to the ADO.NET Entity Framework. My problem is that the xml column is mapped to a string data type. While this is an expected behavior, I'd like to provide my own custom type.

I tried creating a class

public class Address : IXmlSerializable { ... }

and tried replacing the string data type of the address column to my own Address type, but I can't find a way to make the entity framework understand my custom type.

I read about complex type, but it says that the value itself cannot be null, and in my case it can be null.

Is it possible and how?

+1  A: 

You must use string for the mapped property.

You can, however, add a custom (additional) property in a partial class which translates the string into your custom type. So you might do something like:

public partial class Employee
{
    public Address EmployeeAddress 
    {
        get
        {
            return new Address(this.MappedAddressProperty);
        }
    }
}

You can't use these custom properties in LINQ to Entities, though.

Craig Stuntz
I used your idea and I expanded from it. Thank you.
Pierre-Alain Vigeant