3

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.

DMC
  • 361
  • 4
  • 15

2 Answers2

3

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.

Community
  • 1
  • 1
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Interesting! Do you know how NH does implement a complex type collection at the store level? I could image something like a "hidden" table (not corresponding to an entity in the model, similar to a many-to-many join table in EF) and internally managed by the ORM framework. And everytime when the parent entity is loaded the collection is fetched as well by a join in the database. Is it like this or similar? – Slauma Aug 25 '11 at 21:44
  • 1
    @Slauma: a complex type is no diferent from a scalar type. For a collection of ints, you'd have a table with two columns: the value and an FK to the parent. For an attachment, you'd have the FK plus the other 3 columns. Relationships are always one-to-many, not many-to-many, because complex types don't exist by themselves. Loading can be eager or lazy, just like any other collection. – Diego Mijelshon Aug 25 '11 at 21:48
2

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?

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • I was trying to make use of the ComplexType because Attachments in my mind are very similar to the other examples I have seen with ComplexTypes (Names, Addresses, etc) - I thought it would make sense to declare that type once and reuse it multiple places. I think I will keep that, but just wrap the ComplexType in another POCO where I need to. Thanks. – DMC Aug 25 '11 at 21:10