Select ObjectSet by entity type - entity-framework

I need a way to select objects given the name:string of the object and ObjectContext, but dont know how to do this.
I will use this to create a generic lookup dropdown editor template in ASP.MVC
So when view contains #Html.EditorFor (student=>student.School), it will show dropDown containing list of schools.
I get the target entity name from relation.ToMember, but don't know how to query data records with this input.
Currently I have added a custom method which gets string and returns innumerable and inside that I have a big switch case "School": return this.SchooleSet;
Is there a right way to do this.
I also want to add a generic method which allows me to query using syntax like ctx.Select<Teacher>().Where(...)
again here I have implemented with switch but there should be a better way to do this.

Try the CreateObjectSet method.
var q = ctx.CreateObjectSet<Teacher>().Where(...);

Related

How to write an extension for the Entity Framework Core

The actual thing that I need is an effective Delete method for the entities. In other words I would like to be able to write
cntx.Orders.Where(item => item.Category == "Custom1").Delete();
and that is supposed to delete all the records from the table Orders where the Category column value is equal to "Custom1". I don't really care if it will do it right away or after calling cntx.SaveChanges(). And, yes, the query is supposed to be efficient, something like
DELETE FROM Orders WHERE Category = "Custom1"
I know about at least 3 extension libraries for the Entity Framework Core which advertise such abilities but non of them work for Android. Now, I'm thinking how difficult it actually is to write a Delete extension method myself. Can anybody help me with an example? Apparently I should be able to add something to the expression tree which will be called by the framework and in my turn I would generate "DELETE FROM Orders" and then "Where(item => item.Category == "Custom1")" would be replaced by the "WHERE Category = "Custom1""
So, apparently everything should start from
public static class QueryExtension {
public static void Delete<T>(IQueryable<T> objThis) {
// The big mystery is what to call here to ensure that "DELETE FROM [TableName]"
// is entered to the right place of the expression tree and then
// we somehow need to execute the complete statement here or delegate it to SaveChanges
}
}
I sort of realize that translation of the expression tree into a SQL statement happens somewhere in the expression visitor. That is most likely wrapped into some kind of statement compiler of the Entity Framework. I have no idea where all those entry points to write an extension like I need.

how can I get an array of all objects in w2ui

I am trying to subsequently manipulate the menus and tabs of a w2ui application.
In order to implement a generic solution, I added an additional attribute (zndesktop) to the related elements. Now I am looking for a generic method which gives me an array of all objects having this attribute.
Of course, I can hardcode such a query. But I am asking if there is an generic approach (for example w2ui.objects) which would return an array of all UI objects created for the application (recursive search)
w2ui objects are actually stored directly in the global w2ui object using their name, until you destroy them.
Example: if you create a grid with
$('#grid').w2grid({
name: 'my_grid',
...
});
Then you can access it with w2ui.my_grid or w2ui['my_grid'].
Of course you can also iterate the w2ui object (or rather its properties) just like any other JS object.

MagicalRecord: How to import values into a related entity

I'm using a web service which returns results like the following example:
{
"name":"Frank",
"meals":[
"cheeseburger",
"lasagne"
]
}
My Core Data schema looks like this:
Using MagicalRecord's MR_importValuesForKeysWithObject method, how would I set about mapping the meals key to the related Meals.name attribute?
I can map the meals manually after, using a for in loop, but just wondered if there was a way MR_importValuesForKeysWithObject would perform this for me?
Basically I want each object in the JSON "meals" array to become a new Meals entity.
Override importMeals: on the Person object and do the lookup/create/associate manually.
(longer answer)
every property imported via MagicalRecord calls import(PropertyName) on the target object, by implementing it you can override functionality.

How to get Properties from an EObject, similiar to the Properties View

i have an EObject and want to get all the Properties from it. I tryed to get all Structural Features:
myEObject.eClass().getEAllStructuralFeatures()
but i get too many Properties i do not want like the object ID.
With
myEObject.eClass().getEStructuralFeatures()
there are missing some that are displayed in the Properties View.
So how can i get the same List of Properties from an EObject like the Properties View does?
Thx for your help
Just use the first option and filter out the ones you don't want (like object id) from being displayed. It's probably the easiest way.
If it must absolutely be the same as the list that is displayed in the properties view, the safest bet is to find the code used to populate the properties view and reuse it.

Spring List Binding in Form

I'm trying to bind a list/arraylist/hashmap/etc of custom objects to my form in JSP using Spring. Right now, the controller creates a Map of the two lists (Boolean list and custom object list) in referenceData(), and provides it to the form which uses those values to populate the fields. The values are initialized from a MySQL database using Hibernate, and all that works fine. The list is a known length before the form is initialized, so that part is easier. Now what I'd like to do is correctly bind those objects in the form, so that when there are changes made, I can detect that in onSubmit() (or wherever is appropriate), and update the database accordingly. I can't seem to bind them correctly in the form so that I can see changes made. I tried just using a list of the form fields as the model, but even that wasn't working correctly. Do I just need to inject the list in a particular way? Any ideas or examples here? Any help would be greatly appreciated.
UPDATE: At Ralph's request here is the solution I used:
In my data object class, I lazy loaded a map using MapUtils.lazyMap(), with a String key and other custom object value. The other custom object is just a class that contains List<String> and getters/setters. In the corresponding .jsp file, I just nest several loops to loop through the keys first using loop.current.key and then loop2.current.value.paramsList to loop through the values for that key. This was not really what I asked for in my original post, as I was looking for a more general solution, and the lazy loading pointed me in the right direction.
In Spring 2 you need a special List in your Command object, that is able to grow if one add the x-th element event if the list has not this size yet.
One way to do that is to use LayzList decorator from commons-collections.
#Override
protected Object formBackingObject(final HttpServletRequest request)
throws Exception {
List<PosterSelectionRow> posterSelectionRowList = LazyList.decorate(
new ArrayList<PosterSelectionRow>(),
new PosterSelectionRowListFactory());
return new PosterSelectionCommand(posterSelectionRowList);
//PosterSelectionCommand contains a list of selected poster rows
}
private static class PosterSelectionRowListFactory
implements org.apache.commons.collections.Factory {
/** Invoked if the list need a new element */
public Object create() {
return = new PosterSelectionRow();
}
}
When I remember right, there is a way without that Factory stuff, but I am not sure.