locked
Wiring tables in code first fluent api for existing database RRS feed

  • Question

  • Hi,

    I have a following datamodel. I want to be able to query Order details based on OrderID from Orders table. So my code looks like below.

    var myOrders = from t in myContext.Orders
    where t.OrderID == "000408078Y"
    select t;

    Console.WriteLine("START - Writing Order Details");

    foreach (var myOrder in myOrders)
    {
    Console.WriteLine ("Order - {0}", myOrder.OrderID);
    foreach (var myStatusHistory in myOrder.StatusHistory)
    {
    Console.WriteLine("------ {0} , {1} ", myStatusHistory.StatusHistoryID, myStatusHistory.StatusMaster.StatusDesc);
    }
    }

    Console.WriteLine("DONE - Writing Order Details");
    Console.ReadKey();

    The problem now I am facing is that when I hit the line "Console.WriteLine("------ {0} , {1} ", myStatusHistory.StatusHistoryID, myStatusHistory.StatusHistory.StatusDesc);". Since the StatusHistory is a child to Status, there is a collection of "StatusHistory" under status. So if I print myStatusHistory.StatusMaster.Count it will show the number of StatusHistory that matches StatusCode, because of how it is traversed.

    My question, how can I avoid StatusMaster loading all the StatusHistory that matches the statuscode criteria?

    Hope I have explained my problem correctly.

    Thanks,

    Gaja


    Gaja Kannan

    Tuesday, July 17, 2012 4:44 AM

Answers

  • Hi Allen,

         Thanks for responding.  I found the answer.  Basically I have mixed fluent api and codefirst in my application and made the mistake of adding the following to my statushistory class as below

    this.HasRequired(t => t.statusmaster).WithMany(t => t.statushistory);

    This made my myStatusHistory.StatusMaster.Count more than '1' and actually it started to become recursive.  I removed .WithMany and that solved the problem. 

    Thanks,


    Gaja Kannan

    • Marked as answer by Allen_MSDN Friday, July 20, 2012 1:49 AM
    Thursday, July 19, 2012 11:34 PM

All replies

  • Hi Gaja Kannan,

    Welcome to MSDN Forum.

    Based on the table schema you posted, the relationship between 'StatusMaster' and 'StatusHistory' is one to many. So, if you print 'myStatusHistory.StatusMaster.Count', it should always be 1. Or have I miss some thing?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, July 19, 2012 3:10 AM
  • Hi Allen,

         Thanks for responding.  I found the answer.  Basically I have mixed fluent api and codefirst in my application and made the mistake of adding the following to my statushistory class as below

    this.HasRequired(t => t.statusmaster).WithMany(t => t.statushistory);

    This made my myStatusHistory.StatusMaster.Count more than '1' and actually it started to become recursive.  I removed .WithMany and that solved the problem. 

    Thanks,


    Gaja Kannan

    • Marked as answer by Allen_MSDN Friday, July 20, 2012 1:49 AM
    Thursday, July 19, 2012 11:34 PM