Entity Framework Code First Saving type as string

c#, entity-framework-4.1

Solution

One possible way would be to have the `MailAddress` property not mapped to the DB, but have a 2nd property that is. something like:

public class Person
{
    public string EmailString { get; set; }

    [NotMapped]
    public MailAddress Email
    {
        get { return new MailAddress(this.EmailString); }
        set { this.EmailString = value.ToString(); }
    }
}

See here for info on the `[NotMapped]` attribute: How not persist property EF4 code first?

Problem

I am using Entity Framework Code First and have some Classes with a System.Net.Mail.MailAddress type like this. ``` public class Person { public MailAddress Email { get; set; } } ``` When EF tries to create the DB I get this error ``` "Problem in mapping fragments starting at line 6 No mapping for properties Person.Email" ``` How can I tell EF to just save the result of the Email.ToString() to the DB and then I can make a new MailAddress(stringOfEmail); when setting the value on the object? Or am I better off just changing he data type for Email to a string and handle the validation somewhere else?

Original source

Related problems