locked
Association mapped incorrectly by designer? RRS feed

  • Question

  • I'm just getting started with Entity Framework.  I installed the AdventureWorksLT database, created a project, added a new ADO.Net Entity Data Model (pointed to AdventureWorksLT data connection), and can compile and run a small program that gets a Product by ProductID.

    The database has a Product table with PK ProductID and FK ProductModelID, among other fields.  There is also a ProductModel table that has PK ProductModelID, Name, and other fields.

    I want to get the ProductModel Name for the Product I have retrieved.  However, when I try to use Product.ProductModel, I get null.  The data in the database supports this association (Product.ProductModelID == 6 and there is a record in ProductModel table with ProductModelID == 6).  So I would expect this association to work.  Am I misunderstanding something?

    In the designer when I click on the association between Product entity and ProductModel entity it seems to show ProductModel.ProductModelID relating to Product.ProductID, which I don't understand.  This relationship is represented correctly in the database.

    I look in the .edmx and see this:
              <AssociationSetMapping Name="FK_Product_ProductModel_ProductModelID" TypeName="AdventureWorksLTModel.FK_Product_ProductModel_ProductModelID" StoreEntitySet="Product">
                <EndProperty Name="ProductModel">
                  <ScalarProperty Name="ProductModelID" ColumnName="ProductModelID" />
                </EndProperty>
                <EndProperty Name="Product">
                  <ScalarProperty Name="ProductID" ColumnName="ProductID" />
                </EndProperty>
                <Condition ColumnName="ProductModelID" IsNull="false" />
              </AssociationSetMapping>

    Is the designer mapping Product.ProductID to ProductModel.ProductModelID?  That would explain the record not found since there is no ProductModel with ProductModelID equal to the ProductID I'm using.

    Ryan


    Tuesday, June 3, 2008 9:08 PM

Answers

  • In order for the EF to know how Product is related to ProductModel, it needs to be able to go to one place where it can find out which ProductID is related to which ProductModelID.

     

    It can't do this by using the ProductModel table, since this table just contains the ProductModelID column.  It can do this using the Product table, since if it is looking at a certain row in this table, it has both IDs.

     

    So, note the StoreEntitySet for this mapping is pointing to the Product entity set, i.e., the Products table.

     

    For the ProductModel end of the association, we need to tell the EF how the ProductModel entity's keys map to the columns in Products.  In this case, they match up by name.

     

    For the Products end, we do the same, and they also match up by name.

     

    So, this looks to be mapped correctly.

     

    I suspect that the reason why you are getting null is because the EF does not automatically load the other ends of an assocation for you.  I believe that you need to explicitly load the other side of the association before you navigate to it.

    Wednesday, June 4, 2008 12:06 AM

All replies

  • In order for the EF to know how Product is related to ProductModel, it needs to be able to go to one place where it can find out which ProductID is related to which ProductModelID.

     

    It can't do this by using the ProductModel table, since this table just contains the ProductModelID column.  It can do this using the Product table, since if it is looking at a certain row in this table, it has both IDs.

     

    So, note the StoreEntitySet for this mapping is pointing to the Product entity set, i.e., the Products table.

     

    For the ProductModel end of the association, we need to tell the EF how the ProductModel entity's keys map to the columns in Products.  In this case, they match up by name.

     

    For the Products end, we do the same, and they also match up by name.

     

    So, this looks to be mapped correctly.

     

    I suspect that the reason why you are getting null is because the EF does not automatically load the other ends of an assocation for you.  I believe that you need to explicitly load the other side of the association before you navigate to it.

    Wednesday, June 4, 2008 12:06 AM
  • Thanks.  For posterity, here are the things I was missing:

    (Note that I used "designer" a couple times where it's probably some other tool that generates the .edmx from the database.)

    1) I was assuming associations were automatically loaded for me.  So in my case I have to call Product.ProductModelReference.Load() before I can access Product.ProductModel.

    2) In the Designer, when looking at the entity model and I click on an association and look at mapping details pane, I was thinking that would define how the association is determined.  I find the mapping details pane for the association pretty confusing. They seem to imply that I can remap the meanings of entity properties here, and I wouldn't consider that kind of remapping to be part of an association.

    Ryan
    Wednesday, June 4, 2008 2:00 AM