In EF it cannot be donde directly. However, there is a workaround: you can create a collection property which is not mapped to the database, and another property, mapped to the DB, which holds a serialized version of your collection. If the collection has many elements you'll suffer performance problems. If not, it's alright to use it.
Seudocode:
public class Entity
{
[NotMapped]
public List<string> MyStrings
{
get { return Serializer.Deserialize(SerializedMyStrings); }
set { SerializedMyStrings = Serializer.Serialize(value); }
}
// Mapped to DB
public string SerializedMyString { get; set; }
}
NOTE: depending on the serializer implementation the property could be an string
(XML, JSON, concatenation with a special separator, i.e. anything represented by text) or byte[]
(if you use a binary serializer). This data will be stored in single column of your DB table
There are several problems:
- there is an additional property that shouldn't be directly used (the serialzide property). This can be mitigated by making it protected.
- the collection is always loaded (there can be no lazy loading)
- the serialization will degrade the performance