none
What Am I Missing? RRS feed

  • Question

  • Hi;

    I've been using EF for some time now but I always seem to run into problems that take days to figure out. I have to admit I am having problems the intuitiveness of EF. I am currently encountering the following problem:

    Have a EF model (completely generated via interfacing with a SQL Server [CE] DB)
    1) have entity A
    2) have entity B
    3) entity A contains a collection of entity Bs
    4) I create an object of type entity A
    5) I create an object of type entity B
    6) I add entity A to the EF model (the collection that maintains these entites)
    7) I add the entity B object to the collection maintained in entity A
    8) I do a save operation

    At this point I'm getting the following error:

    -----------------------------------------------------------------

    The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

    -----------------------------------------------------------------

    Now I understand that its complaining about the relation ship that exists between entity B and Entity A. Entity B does have a reference to Entity A and it looks that EF cannot save because it cannot resolve the FK relationship between Entity B and Entity A.

    Now, intuitively I assume that EF would handle all this in the background. From my perspective I do not need to know the explicit DB relationships the EF model is based on. I believe that I should be able to add an entity [object] to a list within another [entity] object and EF would persist the infomration properly and update the [entity] objects accordingly.

    What I'm finding is that this does not seem to be the case, and would like to know what I'm missing in understanding how to properly use an EF model.

    Peter


    Tuesday, January 15, 2013 9:16 PM

Answers

  • Ok;

    I figured out that I was doing something to correct the same issue but highe up in my EF model. From what I can tell doing a Refresh(RefreshMode.StoreWins, TARGET) operation where I'm performing it is making the sections below it go out of synch. This operation is being performed because I read that it is an activity to be done when modifications to the EF model is being performed and causing objects to become out of synch. I need to find a new place to perofrm this call in order for the EF model to be prepared properly.

    Peter

    • Marked as answer by Peter Simard Wednesday, January 16, 2013 1:55 PM
    Wednesday, January 16, 2013 1:55 PM

All replies

  • Hi Peter,

    Please show the code for steps 4 - 7.

      

     

    Fernando (MCSD)

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

    Wednesday, January 16, 2013 5:10 AM
  • Hi;

     CatheterDefinition = Catheter.CreateCatheter(1000, "", 1, 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);5) Mode = CatheterMode.CreateCatheterMode(0, 0, ModeSequece, "", 240, false, 0, 0, 0);
    DataServices.DataOperationsManager.EntityDataHandling.AddToCatheters(CatheterDefinition);
    Mode = CatheterMode.CreateCatheterMode(0, 0, ModeSequece, "", 240, false, 0, 0, 0);
    CatheterDefinition.CatheterModes.Add(Mode);
    Mode.PIDs.Add(PID.CreatePID(0, Mode.CatherterModeID, 0, ModeOneSequence, 30, 3, 10));               
    DataServices.DataOperationsManager.EntityDataHandling.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

    The above steps was a generalization of what I'm doing. Explcitly I do the following

    1) Create an entity object of the type Catheter
    2) Add the Catheter entity object to the EF model
    3) Create an entity object of the Type Mode
    4) Add the Mode entity object to Catheter entity object
    5) Create an entity object of the type PID
    6) Add the PID entity object to the Mode entity object
    7) save the EF data model [changes/updates]

    the organization of the data in the DB schema is as follows (and also EF model generated)

    Catheter [1]
    Catheter [2]
                  Model [1]
                  Model [2]
                            PID [1]
                            PID [2]

    I.E.
    1) a Mode is associated to a Catheter and a relationship exists in the DB for it (and the EF model)
    2) a PID is associated to a Mode and a relationship exists in the DB for it (and the EF model)

    The issue I am running into is the PID and mode association. When I add a PID to a Mode - in the way described, I get the error mentioned. If I don't add a PID to a Mode then the save operation works properly. I.E. the Mode association to a Catheter does not cause the same problem. Keep in mind that I'm saving the model after all the objects are created and added to the associated collection.

    Peter

    • Marked as answer by Peter Simard Wednesday, January 16, 2013 1:52 PM
    • Unmarked as answer by Peter Simard Wednesday, January 16, 2013 1:52 PM
    Wednesday, January 16, 2013 11:34 AM
  • Ok;

    I figured out that I was doing something to correct the same issue but highe up in my EF model. From what I can tell doing a Refresh(RefreshMode.StoreWins, TARGET) operation where I'm performing it is making the sections below it go out of synch. This operation is being performed because I read that it is an activity to be done when modifications to the EF model is being performed and causing objects to become out of synch. I need to find a new place to perofrm this call in order for the EF model to be prepared properly.

    Peter

    • Marked as answer by Peter Simard Wednesday, January 16, 2013 1:55 PM
    Wednesday, January 16, 2013 1:55 PM