Question

Example situation (I have asked this more directly and received no feedback, so please excuse the abstract example but I am just trying to pose the question better):

For brevity (actual models are hundreds of lines). A database is modeled to represent houses. A house can have rooms, windows, and yards. A room can have furniture or electronics. Furniture can be a couch, table, or chair. A couch has a pattern, material, and dimensions.

Mr. F. Bar's house is a show room for Bar's Crazy Couches. Every month Mr. Bar's rooms display hundreds of couches. Mr. Bar likes to know when people like his couches, and feedback is kept for every room setup.

Mr. Ed, Mr. Bar's brother, runs his database management system. Mr. Ed decides to remove a couch that hasn't been used for a while!

Assume that this deletion is accepted, or archived, or placed in a partition, or flagged as inactive (i.e. it is not blocked which is an option here).

How can the absence of this dependent couch be determined in a house which has a room that references the removed couch from a linq query using Entity Framework 4.1?

The calling query could look like this (for brevity I have left only the topical parts), but it would have an exception that is hard to catch.

public House getHouse(object id){
 using( DbContext context = new FooBarContext()){
  DbSet<House> dbSet = context.Set<House>();
  IQueryable<House> query = dbSet;
  query = query.Where(h => h.HouseKey == id);
  query = query.Include(h => h.Room);
  query = query.Include(h => h.Room.Couch);
 }
 return query.ToList();
}

var house = getHouse(9).FirstOrDefault();

The house will populate with the correct house. It will include the proper set of rooms. However, one room will have a reference to a couch which is broken. Any try{}catch{} up to this point will result in no exceptions caught. This is just for house with index number 9. At this point in code, this could easily be a list of many different house compositions.

How can I make sure that the house containing a room containing a removed couch is caught?

Was it helpful?

Solution

As you might imagine, this will not be pretty. Basically, you have to do a left join, then access all couches from all the houses/rooms returned. When you try to access the bad couch you'll get a SystemException:

A relationship multiplicity constraint violation occurred: 
    An EntityReference expected at least one related object, 
    but the query returned no related objects from the data store.

try
{
    var test = (from h in context.Set<House>()
                join r in context.Set<Room>()
                  on h.Room.Id equals r.Id into houseRoom
                from joinHouseRoom in houseRoom.DefaultIfEmpty()
                join c in context.Set<Couch>()
                  on r.Couch.Id equals c.Id into houseRoomCouch
                from joinHouseRoomCouch in houseRoomCouch.DefaultIfEmpty()
                select h).ToList()
                         .Select(x => x.Room.Couch.Material)
                         .ToList();
}
catch(SystemException se)
{
    Console.WriteLine(se.Message);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top