none
EF POCO The INSERT statement conflicted with the FOREIGN KEY child RRS feed

  • Question

  • Been struggling with this for a while, hope someone can help me.

    The error I am getting is

    This is using Entity Framework 4 and POCO's

    The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Addresses_Organisations1\". The conflict occurred in database \"COCAMS\", table \"dbo.Organisations\", column 'OrganisationId'.\r\nThe statement has been terminated."

    The application is Windows based app, the code included is stripped of anything I felt was unnecesarry.

    The ObjectContext is a singleton and all I am doing is reading and Organisation, displaying the Organisation and associated Address in a WPF window

    Then updating the Organisation using ApplyCurrentChanges, I tried just calling context.SaveChanges() got the same error;

    Perhaps I am doing something fundamentally wrong, I have never used Entitiy Framweork before, I have no idea why it is trying to do and Insert it should be doing an Update.

    The entity design is Organisation 0 to 1 Addresses

     public class ContextDO
        {
            private static AU.GOV.SA.COURTS.COCAMS.POCO.EntityFramework.COCAMSEntities _context;
    
            public static COCAMSEntities getInstance()
            {
                if (_context == null)
                {
                    _context = new COCAMSEntities();
                }
    
                return _context;
            }
        }
    
    public class OrganisationDO : IDataObject_IntPrimaryKey<Organisation>
        {
            COCAMSEntities _context = ContextDO.getInstance();
    
    
            public Organisation Get(int organisationId)
            {
                var organisation = (from o in _context.Organisations where o.OrganisationId == organisationId select o).FirstOrDefault();
    
                return organisation;
            }
    
            public void Update(Organisation organisation, User user)
            {
                organisation.ModifiedBy = user.UserId;
                organisation.ModifiedDate = DateTime.Now;
                _context.ApplyCurrentValues("Organisations", organisation);
                _context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
            }
    }
    
    public class OrganisationBO:IBusinessObject_IIntPrimaryKey<Organisation>
        {
            public OrganisationDO _organisationDO = new OrganisationDO();
    
            public Organisation Get(int organisationId)
            {
                if (organisationId == null)
                {
                    return null;
                }
                else
                {
                    return _organisationDO.Get((int) organisationId);
                }
            }
    
            public List<ValidationResult> Update(Organisation organisation, User user)
            {
                List<ValidationResult> results = Validate(organisation);
    
                if (results.Count == 0)
                {
                    _organisationDO.Update(organisation, user);
                }
    
                return results;
            }
    
    
            public List<ValidationResult> Validate(Organisation organisation)
            {
                List<ValidationResult> results = organisation.Validate();
    
                //TODO:Add Validation
                return results;
            }
    }
    
    
    private void SaveOrganisation()
    {
         OrganisationBO organisationBO = new OrganisationBO();
         Organisation organisation = organisationBO.Get(((Organisation)dgrOrganisations.SelectedItem).OrganisationId);
         organisation.OrganisationName = "something";
         organisationBO.Update(organisation, (User)Application.Current.Properties["User"]);
    }
    
    
    

     


    Simon
    Thursday, October 27, 2011 4:13 AM

Answers

  • Ah I worked it out, the ObjectContext is a singleton so I dont believe it is detached. But I had a OneWay binding in the XAML to display the Address in a user control

    I was then doing something like

    organisationEntity.Address = addressControl.Address

    this must make it think it is a new address, even though the object entity should be the same object in both contexts.


    Simon
    Friday, October 28, 2011 4:00 AM

All replies

  • Hi Simon,

    Welcome!

    Based on the exception, you may insert a new foreign key which doesn't exsit in the parent table.

    I think you should check if the organisation is new added entity in your update method:

    http://msdn.microsoft.com/en-us/library/bb896248.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, October 28, 2011 2:56 AM
    Moderator
  • Ah I worked it out, the ObjectContext is a singleton so I dont believe it is detached. But I had a OneWay binding in the XAML to display the Address in a user control

    I was then doing something like

    organisationEntity.Address = addressControl.Address

    this must make it think it is a new address, even though the object entity should be the same object in both contexts.


    Simon
    Friday, October 28, 2011 4:00 AM