OpenAccess.RT.sql.SQLException

4 posts, 1 answers
  1. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 08 Aug 2011 Link to this post

    I am trying to insert a new item into the database using OpenAccess.

    Here is my code:
    EntitiesModel context = new EntitiesModel();
      
                Organisation myOrganisation = new Organisation();
      
                myOrganisation.GroupId = 1;
      
                myOrganisation.Details_Name = "testDDDDDDDDDDDDDDDD";
      
                myOrganisation.VisibilityTypeId = 1;
                myOrganisation.UserId = 1; 
      
                myOrganisation.Core_Position = 99;
                myOrganisation.Core_StateFlag = 0;
                myOrganisation.Core_CreationDate = DateTime.UtcNow;
                myOrganisation.Core_ModifiedDate = DateTime.UtcNow;
      
                Office myOffice = new Office();
      
                myOffice.Core_Position = 99;
                myOffice.Core_StateFlag = 0;
                myOffice.Core_CreationDate = DateTime.UtcNow;
                myOffice.Core_ModifiedDate = DateTime.UtcNow;
      
                myOrganisation.Offices.Add(myOffice);
      
                context.Add(myOrganisation);
      
                context.SaveChanges();

    It works fine if i insert only the organisation but if i first add a new office to the organisation and then try to insert it it fails.

    I get the following error:

    failed: Telerik.OpenAccess.RT.sql.SQLException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_OrganisationOffice". The conflict occurred in database "Squirrel", table "dbo.Organisations", column 'Id'.

    What can i do to avoid this?
  2. Answer
    Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 10 Aug 2011 Link to this post

    Hi Andrew,

    In order to resolve the issue you can try the following:

    1) Select the association between Organization and Office classes in the Visual Designer
    2) Find its Target Property group of properties
    3) Set IsManaged to True

    This action will force Telerik OpenAccess ORM to synchronize automatically both sides of the relation, so when you add an Office instance to the Organization.Offices collection, the Office.Organization back-reference will be in sync and insert operations will be handled correctly.

    In case the IsManaged property was already “true” or you have any other questions, do not hesitate to contact us.

    Kind regards,

    Ivailo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. DevCraft banner
  4. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 10 Aug 2011 Link to this post

    Thank you. That fixed it.
    I have to ask though, why is that not set bu default?
    Is there a reason for not using that?
  5. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 15 Aug 2011 Link to this post

    Hi Andrew,

    I agree that the default value might be confusing at first, but managing such a relation automatically means a certain slowdown of your database access due to the heavier queries executed. Unless your database is very small, it’s better to switch this option to True only for the cases where it is required by your particular scenario – otherwise we recommend for the property to be False for a better performance.

    Let me know if you have any further questions.


    Regards,
    Ivailo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Back to Top