Accessing the relationship of a relationship with Entity Framework?

The Include clause can be used in linq to entities query. For example: using (YourDataContext db = new YourDataContext()) { var query = from s in db.Students. Include("Teacher") where s.Teacher.ID == 1 select s; //etc... } I guess that this.

Students is collection pulled into memory already, so you might consider this code in the part you are retrieving students from dbs. If you want to load Teacher of the Student later on ( very importand is that student entities are tracked by ObjectContext!) you can use the Load mehtod on TeacherReference property, but for every student in this. Students collection separately: student.

TeacherReference.Load() Hope this helps.

The Include clause can be used in linq to entities query. For example: using (YourDataContext db = new YourDataContext()) { var query = from s in db.Students. Include("Teacher") where s.Teacher.ID == 1 select s; //etc... } I guess that this.

Students is collection pulled into memory already, so you might consider this code in the part you are retrieving students from dbs. If you want to load Teacher of the Student later on ( very importand is that student entities are tracked by ObjectContext!) you can use the Load mehtod on TeacherReference property, but for every student in this. Students collection separately: student.

TeacherReference.Load(); Hope this helps.

Include isn't necessary if the only reference to Teacher. Id is the where clause of a LINQ to Entities query. Include is only necessary if you examine Teacher.

* when iterating the results. – Craig Stuntz Sep 14 '09 at 13:59 Yes, Include can't be used in a relationship of an object. I don't longer have access to the db, as I am inside the School class.

I can load a reference just fine, but iterating over all elements of a reference to lead one of their references doesn't sound fine and it's going to be rather wasteful. – J. Pablo Fernández Sep 14 '09 at 14:21.

You show this line of code: from student in this. Students where student.Teacher. Id = id select student First, the = should be a == Is that just a typo?

Second, you don't need Include for the following, corrected query, if you don't dereference Teacher later on: var q = from student in SomeObjectContext. Students where student.Teacher. Id == id select student; LINQ to Entities doesn't require Inlcude for just the where clause.

You would need Include if you later iterated the results and dereferenced Teacher: foreach (var student in q) { Console. WriteLn(student.Teacher. Id); } Third, you show this error: System.

NullReferenceException: Object reference not set to an instance of an object. This is not a LINQ to Entities error. Is the error actually on that line of code, or is it somewhere else?

Also, what's this? If it's not an ObjectContext, then you are in likely LINQ to Objects, not LINQ to Entities. In that case, you wouldn't have Include available.

If this is the case, then where does this. Sbudents come from?

As I said, I have that code inside School, so this is a School. No context is available at this moment so you are probably right in saying it's LINQ to Objects. – J.

Pablo Fernández Sep 14 '09 at 14:22 OK, if you're in LINQ to objects then you must either Load (though that does require School to be attached to a live context) or add the Include to whatever populated school originally. – Craig Stuntz Sep 14 '09 at 14:24.

I've found the debugger let me walk throu each iteration of the query: from student in this. Students where student.Teacher. Id == id select student so I've got to see student.Teacher.Id == id many times.

Every time I was debugging it didn't happen and everything worked just fine. I turned the expression into: from student in this. Students where student.

Teacher! = null && student.Teacher.Id == id select student and it not only stopped crashing, but it worked fine as well (all students where selected as expected). I'm not totally sure why.

I can guess why! Because sometimes a Student might have a teacher an sometimes not. Does a teach mandatory for a student?!

And if you claim that it sometimes returns results then that means it is true, sometimes a student doesn't has a teacher. – mosessaur Sep 14 '09 at 23:36 mosessaur, good idea; unfortunately, Teacher is a non-nullable field. But since as others said, this is no longer LINQ-to-Entities, but LINQ-to-Objects, maybe you are right.

Also if for some reason there were some students with null teacher, when I changed the school of all those students, those should be left behind, and they are not. Very puzzling. – J.

Pablo Fernández Sep 15 '09 at 5:46 Be careful. This may "work" by coincidence.It could be that the students where the teacher is null don't happen to be in the class you're trying to load. But you can't count on that.

You need to solve the problem of why the teachers aren't being loaded in the first place, which goes back to your LINQ to Entities query. – Craig Stuntz Sep 15 '09 at 13:57 The teachers are loaded for all students as far as I can tell, either at debug-time or at run-time. It's more likely that I'm getting extra Student objects not properly initialized, but since that doesn't happen when I'm debugging, it's hard to find out the problem.

– J. Pablo Fernández Sep 22 '09 at 6:44.

Entity Data Model Wizard7 in Visual Studio initially generates a 1:1 (one to one) mapping between the database schema and the conceptual schema in most of the cases. In the relational schema, the elements are composed of the tables, with the primary and foreign keys gluing the related tables together. In contrast, the Entity Types define the conceptual schema of the data.

The entity types are an aggregation of multiple typed fields – each field maps to a certain column in the database - and can contain information from multiple physical tables. The entity types can be related to each other, independent of the relationships in the physical schema. Related entities are also exposed similarly – via a field whose name denotes the relation they are participating in and accessing which, instead of retrieving the value from some column in the database, traverses the relationship and returns the entity (or a collection of entities) it is related with.

Entity Types form the class of objects entities conform to, with the Entities being instances of the entity types. Entities represent individual objects which form a part of the problem being solved by the application and are indexed by a key. CustomerEntity, which contains the customer's name from the Customers table, and the customer's address from the Contacts table.

OrderEntity, which encapsulates the orders of a certain customer, retrieving it from the Orders table. The logical schema and its mapping with the physical schema is represented as an Entity Data Model (EDM), specified as an XML file. ADO.NET Entity Framework uses the EDM to actually perform the mapping letting the application work with the entities, while internally abstracting the use of ADO.NET constructs like DataSet and RecordSet.

ADO.NET Entity Framework performs the joins necessary to have entity reference information from multiple tables, or when a relationship is traversed. When an entity is updated, it traces back which table the information came from and issues SQL update statements to update the tables in which some data has been updated. ADO.NET Entity Framework uses eSQL, a derivative of SQL, to perform queries, set-theoretic operations, and updates on entities and their relationships.

Queries in eSQL, if required, are then translated to the native SQL flavor of the underlying database. Entity types and entity sets just form the logical EDM schema, and can be exposed as anything. ADO.NET Entity Framework includes Object Service that presents these entities as Objects with the elements and relationships exposed as properties.

Thus Entity objects are just front-end to the instances of the EDM entity types, which lets Object Oriented languages access and use them. Similarly, other front-ends can be created, which expose the entities via web services (e.g. , WCF Data Services) or XML which is used when entities are serialized for persistence storage or over-the-wire transfer. Entities are instances of EntityTypes; they represent the individual instances of the objects (such as customer, orders) to which the information pertains.

The identity of an entity is defined by the entity type it is an instance of; in that sense an entity type defines the class an entity belongs to and also defines what properties an entity will have. Properties describe some aspect of the entity by giving it a name and a type. The properties of an entity type in ADO.NET Entity Framework are fully typed, and are fully compatible with the type system used in a DBMS system, as well as the Common Type System of the .

A property can be SimpleType, or ComplexType, and can be multi-valued as well. All EntityTypes belong to some namespace, and have an EntityKey property which uniquely identifies each instance of the entity type. SimpleType, corresponds to primitive data types such as Integer, Characters and Floating Point numbers.

ComplexType, is an aggregate of multiple properties of type SimpleType, or ComplexType. Unlike EntityTypes, however, ComplexTypes cannot have an EntityKey. In Entity Framework v1 ComplexTypes cannot be inherited.

All entity instances are housed in EntityContainers, which are per-project containers for entities. Each project has one or more named EntityContainers, which can reference entities across multiple namespaces and entity types. Multiple instances of one entity type can be stored in collections called EntitySets.

One entity type can have multiple EntitySets. Any two entity types can be related, by either an Association relation or a Containment relation. For example, a shipment is billed to a customer is an association whereas an order contains order details is a containment relation.

A containment relation can also be used to model inheritance between entities. The relation between two entity types is specified by a Relationship Type, instances of which, called Relationships, relate entity instances. In future releases, other kinds of relationship types such as Composition, or Identification, may be introduced.

Relationship types are characterized by their degree (arity) or the count of entity types they relate and their multiplicity. However, in the initial release of ADO.NET Entity Framework, relationships are limited to a binary (of degree two) bi-directional relationship. Multiplicity defines how many entity instances can be related together.

Based on multiplicity, relationships can be either one-to-one, one-to-many, or many-to-many. Relationships between entities are named; the name is called a Role. It defines the purpose of the relationship.

A relationship type can also have an Operation or Action associated with it, which allows some action to be performed on an entity in the event of an action being performed on a related entity. A relationship can be specified to take an Action when some Operation is done on a related entity. Cascade, which instructs to delete the relationship instance and all associated entity instances.

For association relationships, which can have different semantics at either ends, different actions can be specified for either end. ADO.NET Entity Framework uses an XML based Data Definition Language called Schema Definition Language (SDL) to define the EDM Schema. The SDL defines the SimpleTypes similar to the CTS primitive types, including String, Int32, Double, Decimal, Guid, and DateTime, among others.

An Enumeration, which defines a map of primitive values and names, is also considered a simple type. Enumerations are unsupported in the current version of the framework. ComplexTypes are created from an aggregation of other types.

A collection of properties of these types define an Entity Type. Facets13 are used to describe metadata of a property, such as whether it is nullable or has a default value, as also the cardinality of the property, i.e. , whether the property is single valued or multi valued.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions