locked
RIA Services and Attribute-Based Mapping (LINQ to SQL) RRS feed

  • Question

  • Hi,
    I use Attribute-Based Mapping (LINQ to SQL) , where in one of the classes I have a EntityRef like so:

    private EntityRef<Address> _address;
    [System.Data.Linq.Mapping.Association(Storage = "_address", ThisKey = "LegalEntityID", OtherKey = "AddressID")]
    public Address Address
    {
    get { return this._address.Entity; }
                set { this._address.Entity = value; }
    }

    I then have a metadata class:

    [System.ComponentModel.DataAnnotations.Association("LegalEntity_Address", "LegalEntityID", "AddressID")]
    [Include]
    public Address Address { get; set; }

    When I compile this I get the following error:
    Value cannot be null.
    Parameter name: key

    If I remove the System.Data.Linq.Mapping.Association i don’t get an error but the Address does not get loaded.

    Any ideas?

    Wednesday, May 27, 2009 10:11 AM

Answers

  • What I meant was that the System.Data.Linq.Mapping.Association attribute already exists in the LTS class as you mentioned earlier.

    System.Data.Linq.Mapping.Association(Storage = "_address", ThisKey = "LegalEntityID", OtherKey = "AddressID")]
    public Address Address
    {

    ...

    so that the framework knows the association.

    System.ComponentModel.DataAnnotations.Association is used to associate Entities from different DomainService.  In that case, you need add [External] attribute. 

    Wednesday, May 27, 2009 6:12 PM

All replies

  • You don't need the Association in the metadata.  Instead, you should make sure that you have a DataLoadOptions in your Get method that eager load the Address, so that they can be serialized to the client side.

    DataLoadOptions loadOptions = new DataLoadOptions();
    loadOptions.LoadWith(p => p.Address);
    Context.LoadOptions = loadOptions;
     
    Wednesday, May 27, 2009 4:39 PM
  • Hey,

    Thanks for the reply. I have tried the sugested solution but did not get it to work.

    The get method:

    public IQueryable<LegalEntity> GetLegalEntitys()
    {
        DataLoadOptions loadOptions = new DataLoadOptions();
        loadOptions.LoadWith<LegalEntity>(p => p.Address);
        Context.LoadOptions = loadOptions;
        return Context.LegalEntitys;
    }

    I wonder, if i remove the association attribute, how does the framework know what to load, since the association defines what keys to use in order to establish a relation.

    Or maybe you ment i should remove the Include attribute? anyway tried that too without any luck.

    Also i tried setting  Context.DeferredLoadingEnabled = false;

    I have use these classes in other projects with the System.Data.Linq.Mapping.Association attribute and they have behaved as expected, i guess i need to use ither System.ComponentModel.DataAnnotations.Association or System.Data.Linq.Mapping.Association, but i cant compile when i use System.Data.Linq.Mapping.Association.

    Thanks / Micke

    Wednesday, May 27, 2009 5:20 PM
  • Doesn't the Association attribute exist in the mapping file generated by Linq To Sql? 

    Wednesday, May 27, 2009 5:28 PM
  • The mapping is not generated, instead the attributes are applied manually.

    Wednesday, May 27, 2009 5:49 PM
  • What I meant was that the System.Data.Linq.Mapping.Association attribute already exists in the LTS class as you mentioned earlier.

    System.Data.Linq.Mapping.Association(Storage = "_address", ThisKey = "LegalEntityID", OtherKey = "AddressID")]
    public Address Address
    {

    ...

    so that the framework knows the association.

    System.ComponentModel.DataAnnotations.Association is used to associate Entities from different DomainService.  In that case, you need add [External] attribute. 

    Wednesday, May 27, 2009 6:12 PM
  • I have a very similar situation, where I am not able to join two simple tables, even when using the DataLoadOptions on the server side.  Here are the specifics:

     - I have two tables, with a one-to-many releationship: Parts & Orders.

    - The Orders table has a foreign key, with a relationship to the primary key of the Parts table (i.e., part_id).

    - When Orders are loaded, I want to join those records with the associated Parts.

    - I'm using Linq2Sql, and everything is building properly.  I can see on the server side where the records are being joined in the context before returning from the LoadOrders() method.

    - I'm using a DataGrid on the client side, with a DDS in the XAML.  It successfully calls the LoadOrders() method, and I see the join on the server.

    - When I display the Orders in the DataGrid, the Part Description does not show up.

    Any idea why I can see the rows being joined on the server-side, but can only see the Orders columns on the client-side?  BTW, if I put a LoadParts() call in the Loaded event handler on the client-side, then the Parts columns show up.  However, it appears as though this downloads ALL parts to the client.  Of course, I only want to load the parts that are referenced by the Orders.

     

    Thursday, June 11, 2009 10:38 PM
  • Did you put [Include] on the Part property in your OrderMetadata?  Can you post your code of GetOrders? 

    How did you set up the binding in your DataGrid?

    Thursday, June 11, 2009 10:52 PM
  • I did not manually put an [Include].  Can you be more specific on where it goes?  Here is the Association in the OrderPartDomainService.cs file:

    [Association(Name="Part_Order", Storage="_Part", ThisKey="part_id", OtherKey="part_id", IsForeignKey=true)]
    
     
    Thursday, June 11, 2009 11:06 PM
  • When I created the Domain Servcie, I did not check the MetaData checkbox, so therefore the MetaData class was not generated.  I recreated the project, and created the MetaData class, inserted the [Include], and now the join works great.

     

    Thursday, June 11, 2009 11:38 PM
  • Hello,

    I've a question. I've already joined 2 tables but I don't know how to join 3 tables. I've Order 1-n OrderDetail and Order 1-n City. I want to select OrderDetail table and CityName from City table where OrderID = '###'. How Linq expression for this (using OrderDetail metadata class - Entity Framework).

    Help me please. Thanks u very much.

    Wednesday, August 12, 2009 4:36 AM
  •  :) I solved my problem
    Instead of using syntax:
    include("Order").include("Order.City") I used:
    include("Order").include("City")

    That's my wrong.

    Wednesday, August 12, 2009 5:44 AM
  • hi

    can anyone tel to me how to one tables in a particular field to relationship with another same field

    and how to get in datagrid

    i am using .net ria service and silverlght 3

     

    Tuesday, September 15, 2009 12:29 AM