Archive

Posts Tagged ‘EF’

SPECIFY MANUAL ASSOCIATION FOR YOUR ENTITY AND RIA SERVICES

February 13, 2010 Leave a comment

Entity framework and WCF ria services start to interest be as an alternate solution for one of my project.
Base on that I start to build a simple Silverlight business project in order to see how it really works. Nothing special in that so far, until the time I add part of my entity model coming from an SQL server database tables. From that database I was simply using 2 tables which have a 1 to many relationships between them, as shown below :


Depending on the way you are naming you field, EF is not capable of determining automatically your tables association, so you need to do it manually. From the sample above, the Event.MachineId field is the foreign key. If you compile your project as it is, you will get a compilation error.

Because the foreign key column is set to one of the PK columns of the Event entity, we need to add the following codes to verify the principal and dependent role of the one-to-many association between the Machine and Event entities. 

Adding manually association in your model:

  • From Visual studio select your model .edmx file
  • Right clik and open it with an XML editor
  • Look for the section CSDL of the EDM then add the following part :

<Association
Name=MachineEvent>
          <
End
Type=testModel.Machine
Role=Machine
Multiplicity=1 />
          <
End
Type=testModel.Event
Role=Event
Multiplicity=* />
          <
ReferentialConstraint>
            <
Principal
Role=Machine>

              <PropertyRef
Name=Id/>

            </Principal>
            <
Dependent
Role=Event>

              <PropertyRef
Name=MachineId/>

            </Dependent>
          </
ReferentialConstraint>
</
Association></Schema>

Note that a DomaineService class template as been added to the server project in order to project the server service code to the client SL application

Ok from that point, simply compile your SL server project side in order that everything gets seen from client part.

Now try to compile the whole client SL application. Hmm now thing start to happen. The compiler return again an error mentioning the following:

“Unable to retrieve AssociationType for association ‘MachineEvent’       BusinessApplication1”

Hmm what happen here as my server project compile without any trouble? Somehow related to how the DomaineService gets projected to client?

Trouble seems to come from RIA services which is not capable to identify again the relation between those 2 tables. To other come this we need also to add manually those associations to the SSDL part of the model

  • From Visual studio select your model .edmx file
  • Right clik and open it with an XML editor
  • Look for the section SSDL of the EDM then add the following part :

<Association
Name=FK_Machine_Event>

<End
Role=Machine
Type=testModel.Store.Machine
Multiplicity=1 />

<End
Role=Event
Type=testModel.Store.Event
Multiplicity=* />

<ReferentialConstraint>

<Principal
Role=Machine>

<PropertyRef
Name=Id />

</Principal>

<Dependent
Role=Event>

<PropertyRef
Name=MachineId />

</Dependent>

</ReferentialConstraint>

</Association>

Recompiling the whole thing works ok now.

Thanks to Lingzhi Sun helping me on this issue


 

Categories: Entity framework Tags: ,