none
Entity Framework: Problem while adding a record to an entity which contain few collection of other related entities RRS feed

  • Question

  • Hi,

    I m using entity framework to generate my model from existing database.

    say there is one generated entity 'orderEntity' which contain properties like:

    column of table: orderId, GrandTotal, CustomerId (FK), PaymentProcessId(FK), OrderStatusId(FK)

    Corresponding to each FK, there is an object property like:

      public virtual Customer Customer{ get; set; }

      public virtual PaymentProcess PaymentProcess{ get; set; }

      public virtual OrderStatus OrderStatus{ get; set; }

    Now when creating an object of 'Order' as:

    Order orderObj = new Order();

    orderObj.GrandTotal = 100;

    orderObj.CustomerId = 1;

    dbContext.Order.Add(orderObj)

    dbContext.SaveChanges();<<<<

    ****************************************

    PROBLEM is, I want to add record in Order table only, but its trying to add record in tables corresponding to (FK) (customer, paymentprocess, orderstatus) tables.
    and producing error for required fields in these tables.

    Searched on web, but did not get any exact solution.

    Please let me know how to proceed??? any work around ???
    thanks in advance...

    .

    Monday, August 12, 2013 2:39 PM

Answers

  • Hi Fusion Coder;

    What the error is stating is that in order to add an order entity to the Order table it must have a relationship to the other three tables customer paymentprocess orderstatus. That can be done by adding three new entities and attaching them to the new order entity of query the database for the three entities and attach them to the new order entity. For example the following should work.

    // Get the entities from the three tables
    var c = (from c in Context.Customer
             where c.CustomerId == 1
             select c).SingleOrDefault();
              
    var o = (from os in Context.OrderStatus
             where ?????
             select os).SingleOrDefault();
    
    var p = (from pp in Context.PaymentProcess
             where ?????
             select pp).SingleOrDefault();
    
    Order orderObj = new Order();
    orderObj.GrandTotal = 100;
    orderObj.Customer = c;
    orderObj.PaymentProcess = p;
    orderObj.OrderStatus = o;
    dbContext.Order.Add(orderObj)
    dbContext.SaveChanges();

    I hope that this helps.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Fusion Coder Tuesday, August 13, 2013 9:44 AM
    Monday, August 12, 2013 5:44 PM

All replies

  • What's the relationsships between the Order entity and the other entities? Can an order exist without a customer, payment process or order status? Are you getting an exception when you try to commit the changes and yf you do, what does it say?
    Monday, August 12, 2013 2:47 PM
  • there is already customer table having a record with customerId =1, than I am uisng to set as:

    orderObj.CustomerId = 1;

    then, while adding order to order table, why its trying to add new customer and then produces error as:

    customerFirstName is field is required, etc..

    Monday, August 12, 2013 3:05 PM
  • Hi Fusion Coder;

    What the error is stating is that in order to add an order entity to the Order table it must have a relationship to the other three tables customer paymentprocess orderstatus. That can be done by adding three new entities and attaching them to the new order entity of query the database for the three entities and attach them to the new order entity. For example the following should work.

    // Get the entities from the three tables
    var c = (from c in Context.Customer
             where c.CustomerId == 1
             select c).SingleOrDefault();
              
    var o = (from os in Context.OrderStatus
             where ?????
             select os).SingleOrDefault();
    
    var p = (from pp in Context.PaymentProcess
             where ?????
             select pp).SingleOrDefault();
    
    Order orderObj = new Order();
    orderObj.GrandTotal = 100;
    orderObj.Customer = c;
    orderObj.PaymentProcess = p;
    orderObj.OrderStatus = o;
    dbContext.Order.Add(orderObj)
    dbContext.SaveChanges();

    I hope that this helps.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Fusion Coder Tuesday, August 13, 2013 9:44 AM
    Monday, August 12, 2013 5:44 PM
  • there is already customer table having a record with customerId =1, than I am uisng to set as:

    orderObj.CustomerId = 1;

    then, while adding order to order table, why its trying to add new customer and then produces error as:

    customerFirstName is field is required, etc..


    Does the Customer property get initialized when you create an Order object, i.e does it have a value other than NULL when you call the SaveChanges method?
    Tuesday, August 13, 2013 7:57 AM