开发者

Entity Framework - How can I create an array of complex property?

开发者 https://www.devze.com 2023-01-21 18:47 出处:网络
In Entity Framework v1, can I create an array of complex property? 开发者_JAVA技巧 Assuming I\'ve a \"Question\" entity can it have an array of \"Answers\" (that compose from text and timestamp)?You d

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

开发者_JAVA技巧

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


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号