Reputation: 361
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.
Upvotes: 3
Views: 2153
Reputation: 52725
It is not possible, but not because of a conceptual mismatch as Slauma said, but because EF does not implement it. Look at this answer for more details on collection support -among other things-.
That said, it's probably better to make it an entity. You don't need to wrap it: you can have a base Attachment
type which is not mapped, and then specific subtypes that are.
Upvotes: 3
Reputation: 177133
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 between TestObject
and Attachment
is like the relationship between an Order
and OrderItem
: An OrderItem
refers uniquely to one Order
(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 the OrderItems
/Attachments
a complex type?
Upvotes: 2