Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 Code First doesn't create column for List<string>

I have been playing around quite a lot with EF4 Code First and I do love it. However, I cannot seem to sort this easy one out.

When trying to create something like this, no columns are created in my database:

    public IList<String> Recievers { get; set; }
    public List<String> RecieversTest { get; set; }

    public virtual List<String> RecieversAnotherTest { get; set; }
    public virtual ICollection<Int32> RecieversAnotherTest { get; set; }

Ive tried Annotations to map it to a different column name, I've tried IEnumerable and all sorts of other collections, but it refuses to create a column for it.

After an hour on google I found one that claims she has done it, but I'm starting to doubt that. Should it even be possible?

I can't really see why it just doesn't create a column and use JSON or CSV. It can't be that rare, can it? In my case i just want to store a list of emails.

What am I missing? The project creates all other types without problems, and I've inspected the database to see how other properties I add to test with gets created, while these gets ignored.

So the problem must lie in some setting I'm missing or some configuration.... EF 4.1 RTW on an SQL Server 2008 db.

like image 843
Claes Lövgren Avatar asked May 14 '11 17:05

Claes Lövgren


1 Answers

I have bad news for you. EF doesn't do anything like that. If you want any serialization and deserialization you must do it yourselves = you must expose and map property with serialized value:

private IList<String> _receivers;
// This will be skipped
public IList<String> Receivers 
{ 
    get
    {
        return _receivers;
    }
    set
    {
        _receivers = value;
    } 
}

// This will be mapped
public string ReceiversSer
{
    get
    {
        return String.Join(";", _receivers);
    }
    set
    {
        _receivers = value.Split(';').ToList();
    }
}

Now ReceiversSer will be mapped to a column in the database.

like image 199
Ladislav Mrnka Avatar answered Nov 08 '22 19:11

Ladislav Mrnka