0

How do you iterate over the entities within a model in mvc 4 using entity framework 5.0? Looking for a more elegant process using linq.

Example: AnimalModel may have Cat, Dog, Pig entities. How would I detect just the entities and ignore other properties in the AnimalModel such as isHarry, Name, isWalking, isJumping. Is there a way to do this without using reflection, something within EF5 that allows for just looking at non-null entity values.

The main reason I am interested in this technique is to reduce code bloat and perform generic CRUD operations on the data across all entities and sub entities.

Possible Reference: link

Community
  • 1
  • 1
Cyber Slueth Omega
  • 399
  • 1
  • 3
  • 19

2 Answers2

1

I can't see how you can achieve this without using reflection at all.

You could try the following : Get all the EF types in the assembly which hosts them e.g.

var types = from t in Assembly.GetExecutingAssembly().GetTypes()
            where t.IsClass && t.Namespace == "NamespaceWhereEFEntitiesLive"
            select t;

You may need to ply around a bit with the above query, but you get the idea.

You can then iterate through the properties of AnimalModel, check whether the property is of any type returned in types. e.g.

foreach(var prop in AnimalModelProperties) {
    if (types.Contains(prop.GetType())
}

Note that the above for loop is a bit of a guess, but the pseudo-code should clarify what I'm looking to explain.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
0

When you use EF to insert/update, it automatically ingores all irrelevant properties. If you want an implementation that takes properties from existing objects, then applies them to the database, you could use the relatively new upsert.


If you want a custom way to upsert a graph of objects...

If you are using either database-first or model-first (if you have an EDMX), you could use T4 templates to generate code that does this.

If you want this technique to support navigation properties, you will need some sort of assumption to prevent loops e.g. update from one to many, not the other way around and not many-to-many properties, or use the EDMX's optional description to place a hint on which navigation properties to visit.

Using reflection is a simpler solution, however, although, even with reflection you'll need to decide which way to go (e.g. using attributes (which you can get the T4s to add via the above assumptions/tricks)).


Alternatively, you could convert this technique (that I wrote) to work with EF, thus explicitly specifying where to visit in the graph in the using code, (using dbset.SaveNavigation(graph, listOfPropertyPaths) instead of writing complex code that assumes what you want it to do when you write dbset.Save(graph) (I have successfully done so in the past, but haven't uploaded it yet).

Also see this related article that I have recently found (I haven't tried it yet).

By the way, null properties do have significance in updating the database, often, you won't want to ignore them.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111