Is it possible to create a property within a POCO that is a collection of ComplexTypes?
[ComplexType]
public class Attachment
{
public string FileName { get; set; }
public byte[] RawData { get; set; }
public string MimeType { get; set; }
}
public class TestObject
{
public TestObject()
{
Attachments = new List<Attachment>();
}
public virtual ICollection<Attachment> Attachments { get; set; }
}
I have a feeling that this isn’t possible… I’ve been doing my best to research it and it seems to me like ComplexTypes are more trouble than they’re worth for several reasons.
Your feeling is right: It isn’t possible. The purpose of a complex type is to embed its properties as columns into the parent type’s table. How could one embed a dynamic collection into a row of a table and what would you expect from a complex type collection in terms of how it is stored?
What you need is actually an ordinary navigation property (basically the
[ComplexType]attribute needs to be removed). In my opnion your relationship betweenTestObjectandAttachmentis like the relationship between anOrderandOrderItem: AnOrderItemrefers uniquely to oneOrder(it has a single foreign key which points to the order) and possibly cascading delete is enabled which ensures that the items are deleted together with their order and which emphasizes the dependency of the items on the order. What else and special do you want to achieve by making theOrderItems/Attachmentsa complex type?