4

I'm using ASP.NET Entity Framework 4.1 MVC 3 (C#)

I want to foreach through all the entities in my DbContext. I need to be able to dynamically refer to my entities in order to make dynamic views.

I have read Lerman's book, two MVC (2 & 3) books, msdn, asp.net, etc. Maybe I am just missing something?

It seems like you might have to use ObjectContext to get to the entities. If that is the right way, I sure can't figure out how to do it. Please help. Thank you.

Benjamin
  • 41
  • 1
  • 1
  • 2

3 Answers3

3

you can also do this(for example):

foreach (var dbItem in dbContext.Items)
{
    //do what you want inside the loop with the dbItem
    sList.Add(new SelectListItem() {Text = dbItem.ItemName, Value = dbItem.ItemTag});
}
GisMofx
  • 982
  • 9
  • 27
1

I am not exactly sure what you are asking. If you want to dynamically reference the DbSets inside of DbContext you could use reflection:

DatabaseContext context = new DatabaseContext();
var contextObject = context as Object;
var contextType = contextObject.GetType();
var properties = contextType.GetProperties();
String result = String.Empty;
foreach (var property in properties)
{
  result += property.Name + "\n"
{

But to be perfectly honest, I do not know what you are asking or what you want. I just saw you had no answers yet so I thought I would give my two cents.

Alec
  • 1,646
  • 3
  • 19
  • 35
  • Thank you. This does get me the names as strings but when I do an Html.DisplayFor(), it shows a bunch of irrelevant properties (because they have been cast as properties I'm guessing?) It would even be helpful if someone could just show me how to manually add specific entities to a collection so that I could use them dynamically in my cshtml views. Sorry, I'm still a beginner with C# and object programming. – Benjamin Jun 15 '11 at 20:36
  • You should post some code that you have, I don't really understand what you have so it is hard for me to infer what you need. – Alec Jun 15 '11 at 20:58
  • 1
    I'd like to do something like: `@foreach (entity in my context) //addresses, phonenumbers, whatever { @RenderPartial( the entity's name as a string for the name of the partial view, the entity itself as the object model for the partial view) }` Then in the partial view maybe this: `@foreach (entity that is a navigation property in this model/entity) { Html.EditorFor(the related nav prop entity) }` It seems so simple. But I am a total beginner so maybe I don't ask in a way that makes sense. Julie Lerman talked about using ObjectStateManager in her EF book. But I don't know how to use it. – Benjamin Jun 16 '11 at 00:25
0

Form your query using Entity Sql and a call to CreateQuery.

See if this gets you started.

http://www.codeproject.com/Questions/208209/Problem-with-Entity-SQL?display=Print

ObjectQuery query = ctx.CreateQuery("SELECT P FROM WebStoreEntities.Customers AS P");

Im not 100% sure how to get the names of the entities - try OpticalDelusions way - but this may help once you have them.

You'll have to dynamically put everything together - but the resulting type you may have a problem casting around, but give it a try.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • Hi thank you. It looks like you've already chosen a specific entity in the snippet you posted. I would like to be able to loop through a collection of entities and generate the html.displayfor/editorfor helpers for each entity in the collection. – Benjamin Jun 15 '11 at 20:31
  • I mentioned above for you to try using OpticalDelusions way to get the names of the entities in conjunction with this method for dynamic querying. This may or may not work for you in the end because of the type of object being returned... so I must ask ... why are you trying to do it this way as opposed to for example..creating a viewmodel containing each model type you have in your database? – Adam Tuliper Jun 16 '11 at 15:35
  • I don't understand that yet. Would a viewmodel solve this? Would I be able to have nested partial views and complex model-binding? – Benjamin Jul 01 '11 at 18:04