This is a migrated thread and some comments may be shown as answers.

OpenAccess.RT.sql.SQLException

3 Answers 130 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andrew
Top achievements
Rank 1
Andrew asked on 08 Aug 2011, 11:16 PM
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?

3 Answers, 1 is accepted

Sort by
0
Accepted
Ivailo
Telerik team
answered on 10 Aug 2011, 08:24 AM
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 >>

0
Andrew
Top achievements
Rank 1
answered on 10 Aug 2011, 11:28 AM
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?
0
Ivailo
Telerik team
answered on 15 Aug 2011, 07:16 PM
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 >>

Tags
General Discussions
Asked by
Andrew
Top achievements
Rank 1
Answers by
Ivailo
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or