How do I foreach through my dbcontext entities in EF 4.1?
Solution 1
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});
}
Solution 2
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.
Admin
Updated on October 17, 2020Comments
-
Admin over 3 years
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.
-
Admin almost 13 yearsHi 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.
-
Admin almost 13 yearsThank 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.
-
Alec almost 13 yearsYou 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.
-
Admin almost 13 yearsI'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. -
Adam Tuliper almost 13 yearsI 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?
-
Admin almost 13 yearsI don't understand that yet. Would a viewmodel solve this? Would I be able to have nested partial views and complex model-binding?