Entity Framework : Get Single object table, when table are linked RRS feed

  • Question

  • Hi there,

    I have a problem that might be too simple? I have let's say two tables A(col1, col2), and B(col3, FK_col1) where the FK_Col1 is the foreign key pointing to A(col1)

    The problem is when I generate the Entity Framwork Context, and using JSon I try to get only the data of table B, (that is only data of col3, and FK_col1. I always end up with data comming from table A.

    So my simple question is how do I get only data from B table.

    For now I use this  : db.TblB.Where(c => c.col3.StartsWith(strx)).Take(5);

    and this is returning all the linked data to table B.

    Another question would be how do I get only col3 data, without the info of FK_col1?

    Thx for any help

    Reporting Solution for BizTalk | follow-us on twitter

    Friday, January 3, 2014 12:26 AM


  • Hello BizTalk - Paris,

    >>So my simple question is how do I get only data from B table.

    In entity framework, it will load data with lazy load model by default which will load related entities automatically. To disable it, we just need to configure it in the Constructor like below:

    public DataBaseFirstDBEntities()
                : base("name=DataBaseFirstDBEntities")
                this.Configuration.LazyLoadingEnabled = false;

    For more information regarding load related entities, you can refer to link below, it has a detail introduce about it:


    >>Another question would be how do I get only col3 data, without the info of FK_col1?

    It seems that you want to get specific field in entity framework, for this, we can do it like below:

    var result = db.Orders.Select(order => new { order.OrderID }).FirstOrDefault();

    It will only return the OrderID field. However, the returned type is an anonymous type, so we need to create an OrderDTO class additionally like below:

    class OrderDTO
            public int OrderID { get; set; }
            public string OrderCode { get; set; }
            public string OrderName { get; set; }
    Then the query is like below:
    OrderDTO orderDTO = db.Orders.Select(order => new OrderDTO { OrderID = order.OrderID}).FirstOrDefault();

    There are some discussion regarding it:




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 3, 2014 7:50 AM