Entity Framework Associations

I've created a Model with Entity Framework from three database tables:

  1. Agents
  2. AgentsGroups
  3. Groups

AgentsGroups is a simple table with three columns: 1 id and two foreign keys linking Agents and Groups (every Agent can have multiple Groups). Pretty basic stuff.

Entity Framework correctly recognizes the relationships between the table. Now, with LINQPad I am able to get the names of all the groups associated with an agent starting from the agent ID:

from a in Agents join ag in AgentsGroups on a.Code equals ag.AgentCode join g in Groups on ag.GroupCode equals g.Code where a.Code == 10199 select g.Name

This, though, doesn't work on the very program as, in fact, AgentCode and GroupCode are mapped as Associations, not fields.

I guess I have to use Include, but I've never used it, so the help requested is: how could I translate the given semi-working linq expression in a similar expression giving out the Group Names but using Associations?

Thanks in advance

-------------Problems Reply------------

Much simpler in EF:

from a in Agents
where a.Code == 10199
from g in a.Groups
select g.Name

You almost never use join in EF.

Category:c# Views:0 Time:2009-07-29

Related post

  • Entity Framework vs LINQ to SQL 2008-08-12

    Now that .NET v3.5 SP1 has been released (along with VS2008 SP1), we now have access to the .NET entity framework. My question is this. When trying to decide between using the Entity Framework and LINQ to SQL as an ORM, what's the difference? The way

  • Expression.Invoke in Entity Framework? 2008-08-14

    The Entity Framework does not support the Expression.Invoke operator. You receive the following exception when trying to use it: "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities. Has anyone got a workaround for this missin

  • Update database schema in Entity Framework 2008-08-15

    I installed VS SP1 and played around with Entity Framework. I created a schema from an existing database and tried some basic operations. Most of it went well, except the database schema update. I changed the database in every basic way: added a new

  • How to customize Entity Framework classes? 2008-09-02

    Is there a way to take over the Entity Framework class builder? I want to be able to have my own class builder so i can make some properties to call other methods upon materialization or make the entity classes partial. --------------Solutions-------

  • ADO.NET Entity Framework tutorials 2008-09-15

    Does anyone know of any good tutorials on ADO.NET Entity Framework? There are a few useful links here at Stack OverFlow, and I've found one tutorial at Jason's DotNet Architecture Blog, but can anyone recommend any other good tutorials? Any tutorials

  • Using MySQL with Entity Framework 2008-09-16

    Can't find anything relevant about Entity Framework/MySQL on Google so I'm hoping someone knows about it. --------------Solutions------------- It's been released - Get the MySQL connector for .Net v6.5 - this has support for [Entity Framework] I was

  • Entity Framework and Application Architecture (loose coupling, etc) 2008-09-17

    I am considering to apply Entity Framework in a new project because I liked its OR/M-API as well as the storage/conceptual model mapping-capabilities (plus Linq of course and Entity SQL). But how can loose coupling be achieved betwen the UI layer and

  • Entity Framework: how to return a base type from L2E 2008-09-20

    Considering the following architecture: a base object 'Entity' a derived object 'Entry:Base' and a further derived object 'CancelledEntry:Entry' In EntitySQL I can write the following: [...] where it is of (only MyEntities.Entry) [...] to return only

  • ADO.NET Entity Framework and identity columns 2008-09-23

    Is the Entity Framework aware of identity columns? I am using SQL Server 2005 Express Edition and have several tables where the primary key is an identity column. when I use these tables to create an entity model and use the model in conjunction with

  • SQL Anywhere 11 (Sybase) with Entity Framework in Visual Studio SP1? 2008-09-24

    Well, the question is pretty much in the title. I've just installed Visual Studio SP1, and now when I want to import a Entity Model from a database, it doesn't display the SQL Anywhere provider anymore. Does anyone know if there is a patch or some wa

  • Nullable entity projection in Entity Framework 2008-09-26

    I have a following SQL Server 2005 database schema: CREATE TABLE Messages ( MessageID int, Subject varchar(500), Text varchar(max) NULL, UserID NULL ) The column "UserID" - which can be null - is a foreign key and links to the table CREATE TABLE User

  • How to relate objects from multiple contexts using the Entity Framework 2008-10-01

    I am very new to the entity framework, so please bear with me... How can I relate two objects from different contexts together? The example below throws the following exception: System.InvalidOperationException: The relationship between the two objec

  • Best Practices for Entity Framework and ASP.NET 2008-10-02

    I've been driving myself crazy trying to get the Entity Framework to work as expected (or at least as I expect) in an ASP.NET environment, specifically dealing with objects belonging to different contexts when attempting to save to the database. What

  • Row Level Security with Entity Framework 2008-10-04

    I've been trying to consider how Row Level Security could be implemented with the Entity Framework. The idea is to have a database agnostic means that would offer methods to restrict the rows coming from the ObjectContext. Some of my inital ideas hav

  • How can I force a complete load along a navigation relationship in Entity Framework? 2008-10-05

    Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My test case right now includes a SQL Server 2008 database with 2 tables, Member and Profile, with a 1:1 relationship. I then used the Entity Data Model wizard to auto-generat

  • Is the Entity Framework basically another CRUD code generator? 2008-10-06

    Is entity framework just a fancy name for another CRUD code generator? Or is there more to it? --------------Solutions------------- Thats sort of like saying object oriented programming is basically proceedural with a few modifications. While EF is N

  • For what type of project is Entity Framework currently suited? 2008-10-07

    I was listening to a podcast recently that was discussing at length the short comings of Entity Framework (EF). But, their opinions may need to be taken with a grain of salt (by me), as from what I could gather: These were folks that were ORM experts

  • Db schema is complete, can Entity Framework generate Storage Schema? 2008-10-10

    If I already have a database schema complete, can the entity framework somehow generate the storage schema for me? Or if I have the classes developed, can it generate the other xml's desribing the classes and their relationships? --------------Soluti

  • Building a Decoupled N-Tier App With Entity Framework and VB.NET 2008-10-11

    So we are building an application with a UI Layer (web, mobile, Ajax client, etc) Service/API Layer Business Logic Layer Data Access Layer Our goal is to have an Entity Framework dependency from the Service Layer on down to the DAL. That means the Se

  • How do I set the login info for SQL Server 2008 in Entity Framework? 2008-10-12

    Okay I'm still learning the ropes with the Entity Framework and I came across one more small issue. When I create my Entity Model from the SQL Server 2008 database it warns me about saving the login information in the connection string in the config

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.174 (s). 11 q(s)