none
Entities one to many RRS feed

  • Question

  • Hi

    I am using EF 3.5, as we need to use this for the site...

    Now, I have the following entities/DB tables:

    Person

    lGenderType

    lMaritalStatus

    lPlatform

    Quote

    Now a person has a list of quotes to it, the other tables are also linked to it.

    I have searched for the problems, but the thing is not working when I try to save a person object.

    I have tried attaching the other tables to the same context, as I need to get the Gender, Marital etc from classes (Hence using different contexts).

    If you need some code I will send through... but I have a feeling it has to do with the use of different contexts, and frankly, have no idea how to solve this.

    Thanks


    Friday, January 18, 2013 6:02 AM

Answers

  • Hi;

    The exception you are getting is :

    "A relationship from the 'FK_Product_COmpany' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Product' must also in the 'Added' state."

    The code you have posted does not seem to be anything to do with Product and Company and the relationship between them. I also think that you are adding a new Company without also adding a new Product or so it seems from the exception. Please check that out.

      


    Fernando (MCSD)

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

    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:34 AM
    Monday, January 21, 2013 4:45 PM

All replies

  •   

    The description of what your trying to do is to vague can you please be more specific in your description. Please provide code and the error that is returned or the results you were not expecting.

      


    Fernando (MCSD)

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

    Friday, January 18, 2013 6:41 PM
  • Hi,

    ok, let me start with this.

    I have a n-tier application, the DAL, the BLL and the front-end. I also have some other classes etc.

    So from the front end the person will request a quote, will call the BLL, and through to the DAL to retrieve the data. But now, when I fill the constant entity types of the person, like the person's gender, suburb etc, I use different contexts for each of these, as I have a using(context) in my DAL. So each method in each class of the DAL has a using statement.

    public Entities.lMaritalStatusType GetMaritalStatusType(int p_Pk)
            {
                try
                {
                    using (Entities.HippoDataBase db = new Entities.HippoDataBase())
                    {
                        var query = from a in db.lMaritalStatusTypes
                                    where a.Pk == p_Pk
                                    select a;
                        
                        Entities.lMaritalStatusType marital = new Entities.lMaritalStatusType();
                        marital = query.FirstOrDefault<Entities.lMaritalStatusType>();
                        db.ObjectStateManager.ChangeObjectState(marital, System.Data.EntityState.Detached);
                        return marital;
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }

    So from the example above, I add this to the person entity, but now I tried using EF 4, and changing the object state, when I try and save the person object I get the following error:

    "A relationship from the 'FK_Product_COmpany' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Product' must also in the 'Added' state."

    I suspect that one of the entities is in an added state? The product has a sub entity of company.

    Now, is there a way I can have one context doing a quote? As mentioned before, each person has a list of Quote entities. So a person can have multiple quotes.

    So basically when I try to save a Person object I get the error above. See code below:

    db.People.AddObject(p_Person);                    
                        db.ObjectStateManager.ChangeObjectState(p_Person.lGenderType, System.Data.EntityState.Unchanged);
                        db.ObjectStateManager.ChangeObjectState(p_Person.lMaritalStatusType, System.Data.EntityState.Unchanged);
                        db.ObjectStateManager.ChangeObjectState(p_Person.lQuotePlatform, System.Data.EntityState.Unchanged);                    
                        
                        foreach (Entities.Quote quote in p_Person.Quotes)
                        {                             
                            db.ObjectStateManager.ChangeObjectState(quote.Product, System.Data.EntityState.Unchanged);                         
                            
                        }   
                        
                        db.SaveChanges();


    • Edited by Kobi-wan Monday, January 21, 2013 9:50 AM
    Monday, January 21, 2013 5:05 AM
  • Hi;

    The exception you are getting is :

    "A relationship from the 'FK_Product_COmpany' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Product' must also in the 'Added' state."

    The code you have posted does not seem to be anything to do with Product and Company and the relationship between them. I also think that you are adding a new Company without also adding a new Product or so it seems from the exception. Please check that out.

      


    Fernando (MCSD)

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

    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:34 AM
    Monday, January 21, 2013 4:45 PM