Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

Association in Entity Framework | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » Association in Entity Framework

Association in Entity Framework








Article Posted On Date : Wednesday, March 21, 2012


Association in Entity Framework
Advertisements

Association defines a relationship between two entities in Entity Framework. Association is defined in a conceptual model by the "Association" Element and each relationship contains two ends that describe the entity type and multiplicity type (one to one, zero-or -one, one to many, many to many). The relationship may be managed by the "Referential" constraint. Referential Constrains contains information about a principal role and a dependent role in a relationship.

Entity Framework supports two type of association: Foreign key columns Association and Independent association.

Foreign key association

You may create or change a relationship by modifying the foreign key value of the dependent object when the foreign key properties are included in EDM. This type of association is called a foreign key association. In this type of association, a foreign key must be exposed.
Example:



In this current example, a foreign key is defined between Customer and People (Person in EDM) in the storage model, which means the foreign key is defined in the database.

The following are the storage model and conceptual model. In both storage and conceptual models the foreign key is defined. In the following sample association set, the association and navigation properties are automatically bound or created.

<edmx:StorageModels>
    <Schema Namespace="AdventureWorksModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008"
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
        <EntityContainer Name="AdventureWorksModelStoreContainer">
..........
..........
<AssociationSet Name="FK_Customer_Person" Association="AdventureWorksModel.Store.FK_Customer_Person">
            <End Role="Person" EntitySet="Person" />
            <End Role="Customer" EntitySet="Customer" />
          </AssociationSet>
        </EntityContainer>
...........
...........
<Association Name="FK_Customer_Person">
          <End Role="Person" Type="AdventureWorksModel.Store.Person" Multiplicity="1" />
          <End Role="Customer" Type="AdventureWorksModel.Store.Customer" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Person">
              <PropertyRef Name="PersonId" />
            </Principal>
            <Dependent Role="Customer">
              <PropertyRef Name="CustomerID" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
.......
.......
</Schema></edmx:StorageModels>
<edmx:ConceptualModels>
      <Schema Namespace="AdventureWorksModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"
xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
        <EntityContainer Name="AdventureWorksEntities" annotation:LazyLoadingEnabled="true">
.........
.........
<AssociationSet Name="FK_Customer_Person" Association="AdventureWorksModel.FK_Customer_Person">
            <End Role="Person" EntitySet="People" />
            <End Role="Customer" EntitySet="Customers" />
          </AssociationSet>
.......
.......
<Association Name="FK_Customer_Person">
          <End Type="AdventureWorksModel.Person" Role="Person" Multiplicity="1" />
          <End Type="AdventureWorksModel.Customer" Role="Customer" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Person">
              <PropertyRef Name="PersonId" />
            </Principal>
            <Dependent Role="Customer">
              <PropertyRef Name="CustomerID" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
.......
.......
        </Schema>
    </edmx:ConceptualModels>

Independent association

In Independent association, there is a foreign key property missing from the storage Model (dependent entity). In other words, in this type of association there is no physical foreign key in the database. The relation between two entities is defined as a separate object and handle by the Object State Manager. It has its own object state which must be handled correctly. Here both entities work as detached entities, so it must be handled correctly. When creating a new relationship, the new association must have entities at both ends. The Entity Framework allows us to define a relationship in the conceptual Model.

Example :

In the following example, there are two tables; Employee and Department. An employee must have a department. But a relationship between Employee and Department is not defined in the physical layer (which means the foreign key is not defined in the database).



 

In independent association, the foreign key (Association) is defined in the conceptual model.

To define the association in a conceptual model, we must add association set, association and navigation properties.

The steps for defining an association.

Step 1 :  Define the Association Set in the entity container.

<AssociationSet Name="FK_Employees_Departments" Association="AdventureWorksModel.FK_Employees_Departments">

            <End Role="Department" EntitySet="Departments" />

            <End Role="Employee" EntitySet="Employees" />

</AssociationSet>

 Step 2 : Define the association.

<Association Name="FK_Employees_Departments">
       <End Type="AdventureWorksModel.Department" Role="Department" Multiplicity="1" />
       <End Type="AdventureWorksModel.Employee" Role="Employee" Multiplicity="*" />
       <ReferentialConstraint>
          <Principal Role="Department">
<PropertyRef Name="DepartmentId" />
          </Principal>
          <Dependent Role="Employee">
            <PropertyRef Name="DepartmentId" />
          </Dependent>
       </ReferentialConstraint>
</Association>

Step 3 : Define the navigation property in each entity type in a concepual model.



  <NavigationProperty Name="Emplyees" Relationship="AdventureWorksModel.FK_Employees_Departments" FromRole="Department" ToRole="Employee" />
<NavigationProperty Name="Deparment" Relationship="AdventureWorksModel.FK_Employees_Departments" FromRole="Employee" ToRole="Department" />


Conclusion : The Entity Framework allows us to define a relationship (association) in a conceptual model with a defined foreign key in the physical layer (database).






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2025 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2025

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2025 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.