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

Insert new Record - Error

1 Answer 52 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.
Ingo Dignas
Top achievements
Rank 1
Ingo Dignas asked on 18 Jun 2009, 03:51 PM
I have an Object called "Contact" (name, lastname ect.) This object includes a country object. On my first web form I collect the users data and put it into the objects:

Contact myContact = new Contact();
 int cid =int.Parse(rcb_country.SelectedValue.ToString());
IObjectScope scope1 = ObjectScopeProvider1.GetNewObjectScope();
          var cselected = from a in scope1.Extent<Country>()
                            where a.CountryID == cid
                            select a;
scope1.Transaction.Begin();
foreach (Country coun in cselected)
                myContact.Country = coun;
scope1.Transaction.Commit();


Ok....this works fine.
On my last page I try to insert the contact into my database, like this:

if (cb_agb.Checked)
{
                IObjectScope scope2 = ObjectScopeProvider1.GetNewObjectScope();
                try
                {
                   
                    scope2.Transaction.Begin();
                    scope2.Add(myContact);
                    scope2.Transaction.Commit();
                }
                catch (Exception ex)
                {
                    scope2.Transaction.Rollback();
 
                }
}

Now I get an error: object references between two different objects scopes are not allowed.........the object ...country is already managed

What can it be?



1 Answer, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 23 Jun 2009, 02:17 PM
Hi Joerg Hermann,
you are getting this exception because you are indeed trying to work with two different scopes. To avoid this you should not create a new instance of the scope but use the current one instead. This can be done by modifying your code a little:
IObjectScope scope2 = ObjectScopeProvider1.ObjectScope(); 
This way you will get a reference to your current scope instead of creating a second one.
All the best,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
General Discussions
Asked by
Ingo Dignas
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or