views:

31

answers:

1

In Entity Framework v1, can I create an array of complex property?

Assuming I've a "Question" entity can it have an array of "Answers" (that compose from text and timestamp)?

A: 

You don't need an array of properties, just a simple navigational one-to-many property would suffice. Your Question entity will have a collection of Answer entities.

At the same time, at the database level, your Answer(s) table should have a foreign key QuestionId connecting it to the Question(s) table.

If you are generating the model from your database, and the foreign key is set up correctly, the navigation property should be generated for you by EF. It might not be correctly named however (AnswerSet or smth), but you can rename it to Answers yourself. Later on, you can access the answers via the Question object, e.g.:

var question = context.Questions.Include("Answers").FirstOrDefault(q => q.Id == 1);
bool hasAnswers = question.Answers.Any();
Yakimych
Thanks, but in my domain specific problem I do need an array. can it be done?
Eden
@Eden - how are you planning on storing it in the DB?
Yakimych
@Yakimych - I have 2 tables with one-to-many relationship. This is standard, and it will be best modeled as an array of complex properties as opposed to navigational property.
Eden
@Eden - It seems that you have a simple situation indeed. 2 tables with one-to-many relationship is modeled as a navigational property with a collection on one end and one entity on the other end. *This* is standard. Complex properties are normally flattened out within one table (e.g. if you have a `Company` entity with a complex property `Address` that has `Line1`, `Line2`, `Zip`, etc., you will get a `Company` table with fields `Address_Line1`, `Address_Line2`, `Address_Zip`, etc.). What you are trying to do feels more like reinventing the bicycle. Do it the simple common way instead.
Yakimych