locked
Display foreign key value from entity model RRS feed

  • Question

  • i have a simple demo app with two tables. car and carmaker. the car table has a fk to carmaker. i created my entity model including these two tables. on the server side i can access the attributes this way:

    string carmaker_name = this.Context.Cars.Include("Carmakers").First().Carmaker.name;
    => "BMW"
    but i'm not able to display the carmakers name in my datagrid. the data is returned like this:
    return this.Context.Cars.Include("Carmakers");
    displaying the car attributes is working like expected:
    Binding="{Binding horsepower}"
    trying to display a carmaker attribute displays nothing:

    Binding="{Binding Carmaker.name}"
    what am i missing at this point?
    Wednesday, August 19, 2009 7:05 AM

Answers

  • You will be able to use Carmaker as a seperate entity.  If you created a seperate view then this would be exposed as a seperate entity.

    With regards your "Why include [Include]" questions, see here : http://silverlight.net/forums/t/112977.aspx

    Please mark as answer if this helps.

    Wednesday, August 19, 2009 7:42 AM

All replies

  • Have you add [Include] attribute to your metadata?

    Wednesday, August 19, 2009 7:09 AM
  • if i add [Include] to my metadata for Carmaker

     

    [Include] 
    public Carmaker Carmaker;
      

    i'll get the following error:

    Error 1 The entity type 'TestSL.Web.Carmaker' is exposed by multiple DomainService types. Entity types cannot be shared across DomainServices.   

     

    Wednesday, August 19, 2009 7:24 AM
  • Don't you mean to add the Include statement to Car and not Carmaker?

    Wednesday, August 19, 2009 7:31 AM
  • Ok. that was my fault. i added a second domain service for editing the carmakers. now it's working.

    but this leads to a 2nd question. does that mean that i'm only able to expose the carmakers once? what if i like to create a seperate view for crud'ing my carmakers? they cant be exposed cause they're already bound within my cars?

    and why do i have to include them twice? once in my entity query and second time in the metadata. for me all this doesnt fell really "natually".

    Wednesday, August 19, 2009 7:36 AM
  • You will be able to use Carmaker as a seperate entity.  If you created a seperate view then this would be exposed as a seperate entity.

    With regards your "Why include [Include]" questions, see here : http://silverlight.net/forums/t/112977.aspx

    Please mark as answer if this helps.

    Wednesday, August 19, 2009 7:42 AM
  •  Ok. i see. there's a lot more to learn at this point. but response time in here looks quite impressive. thanks four you help. :)

    Wednesday, August 19, 2009 8:11 AM
  • Thats what this forum is for :o)

    Wednesday, August 19, 2009 8:17 AM
  • and why do i have to include them twice? once in my entity query and second time in the metadata. for me all this doesnt fell really "natually".

    The entity attribute prunes the server side entity tree by generating a smaller entity representation on the client. The query attribute prunes the database resultset, so not everything is pulled into memory. Two different use cases, but similar objectives: performance/memory usage.

    Imagine having the Include-attribute at the entity level, but you won't populate it from the query: even more confusing for the consumer of the entity ;-)

    I think it would have felt more natural if the RIA Include-attribute would have been inverted and renamed to "Exclude" or "ExcludeFromMarshalling". 

    Wednesday, August 19, 2009 8:22 AM
  • but this leads to a 2nd question. does that mean that i'm only able to expose the carmakers once? what if i like to create a seperate view for crud'ing my carmakers? they cant be exposed cause they're already bound within my cars?

    I have just tested it since I was curious and it's really strange you can't expose an entity twice This for instance won't compile:

     

    public class CustomerEntity
    {
        [Required()]
        [Key]
        [StringLength( 10 )]
        public string CustomerId
        {
            get;
            set;
        }
        //....etc
    }
    public class OrderEntity
    {
        [Required()]
        [Key]
        public long OrderId
        {
            get;
            set;
        }
    
        [Required()]
        [StringLength( 10 )]
        public string CustomerId 
        {
            get;
            set;
        }
    
    
        //[Required()]
        [Association( "Order_Customer", "CustomerId", "CustomerId", IsForeignKey = true )]
        [Include]
        public CustomerEntity Customer
        {
            get;
            set;
        }
        //....etc
    }
    
    [EnableClientAccess()]
    public class OrderEntryDomainService : DomainService
    {
        public IEnumerable<OrderEntity> GetPendingOrders(string customerId)
        {
            return null;
        }
    
        public OrderEntity GetOrderByKey(long orderId)
        {
            return null;
        }
    }
    
    
    [EnableClientAccess()]
    public class CustomerDomainService : DomainService
    {
        public IEnumerable<CustomerEntity> GetCustomerByKey(string customerId)
        {
            return null;
        }
    }
    
     

    So merely the fact that you expose a QUERY-operation in another domain service, results in the following build error in the linked Silverlight client project:

    The entity type 'Xxx.SalesOrders.CustomerEntity' is exposed by multiple DomainService types. Entity types cannot be shared across DomainServices.

    If there would have been a restriction for the orderlines, it would have made sense, but for customer, product, addresses, no!

    Wednesday, August 19, 2009 8:38 AM
  • I believe that you could expose multiple DomainService types in Silverlight 2.

    Wednesday, August 19, 2009 8:52 AM