views:

386

answers:

1

I am having a hard time with ADO.NET Data Services (formerly code-named Astoria) as it gives me a bogus exception when I try to insert a new entity from the silverlight client and trying in a clean project (the same code) doesn't.

In both cases, however, data is correctly inserted into the database. Using Fiddler (an HTTP debugger I could see that there is no problem in the HTTP communication as I will show later in this question.

The code :

var ctx = new MyProject123Entities(new Uri("http://andreiri/MyProject.Data/Data.svc"));

var i = new Zone()
{
  Data = DateTime.Now,
  IdElement = 1
};

ctx.AddToZone(i);

i.StareZone = new StareZone() { IdStareZone = 1 };
ctx.AttachTo("StareZone", i.StareZone);
ctx.SetLink(i, "StareZone", i.StareZone);

i.TipZone = new TipZone() { IdTipZone = 1 };
ctx.AttachTo("TipZone", i.TipZone);
ctx.SetLink(i, "TipZone", i.TipZone);

i.User = new User() { IdUser = 2 };
ctx.AttachTo("User", i.User);
ctx.SetLink(i, "User", i.User);

ctx.BeginSaveChanges(r =] ctx.EndSaveChanges(r), null);

when run the last line (ctx.EndSaveChanges(r)) will throw the following exception :

System.Data.Services.Client.DataServiceRequestException was unhandled by user code
  Message="An error occurred while processing this request."
  StackTrace:
       at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.HandleBatchResponse()
       at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.EndRequest()
       at System.Data.Services.Client.DataServiceContext.EndSaveChanges(IAsyncResult asyncResult)
       at MyProject.MainPage.[]c__DisplayClassd6.[]c__DisplayClassd8.[dashboard_PostZoneCurent]b__d5(IAsyncResult r)
       at System.Data.Services.Client.BaseAsyncResult.HandleCompleted()
       at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.HandleCompleted(PerRequest pereq)
       at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.AsyncEndRead(IAsyncResult asyncResult)
       at System.IO.Stream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
       at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.AsyncEndGetResponse(IAsyncResult asyncResult)
  InnerException: System.InvalidOperationException
       Message="The context is already tracking a different entity with the same resource Uri."
       StackTrace:
            at System.Data.Services.Client.DataServiceContext.AttachTo(Uri identity, Uri editLink, String etag, Object entity, Boolean fail)
            at System.Data.Services.Client.MaterializeAtom.MoveNext()
            at System.Data.Services.Client.DataServiceContext.HandleResponsePost(ResourceBox entry, MaterializeAtom materializer, Uri editLink, String etag)
            at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.[HandleBatchResponse]d__1d.MoveNext()
       InnerException:

(there is no further information regarding the exception although the ADo.NET Data Service is configured to return detailed informations)

However the row is inserted correctly and completely in the database. Using fiddler I can see that the request :

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"    
  xmlns="http://www.w3.org/2005/Atom"&gt;
  <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="MyProject123Model.Zone" />
  <title />
  <updated>2009-09-11T13:36:46.917157Z</updated>
  <author>
    <name />
  </author>
  <id />
  <link href="http://andreiri/MyProject.Data/Data.svc/StareZone(1)" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/StareZone" 
    type="application/atom+xml;type=entry" />
  <link href="http://andreiri/MyProject.Data/Data.svc/TipZone(4)" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/TipZone" 
    type="application/atom+xml;type=entry" />
  <link href="http://andreiri/MyProject.Data/Data.svc/User(4)" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/User" 
    type="application/atom+xml;type=entry" />
  <content type="application/xml">
    <m:properties>
      <d:Data m:type="Edm.DateTime">2009-09-11T16:36:40.588951+03:00</d:Data>
      <d:Detalii>aslkdfjasldkfj</d:Detalii>
      <d:IdElement m:type="Edm.Int32">1</d:IdElement>
      <d:IdZone m:type="Edm.Int32">0</d:IdZone>
      <d:X_Post m:type="Edm.Decimal">587647.4705</d:X_Post>
      <d:X_Repost m:type="Edm.Decimal" m:null="true" />
      <d:Y_Post m:type="Edm.Decimal">325783.077599999</d:Y_Post>
      <d:Y_Repost m:type="Edm.Decimal" m:null="true" />
    </m:properties>
  </content>
</entry>

is well accepted and a successful response is returned :

HTTP/1.1 201 Created
Date: Fri, 11 Sep 2009 13:36:47 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
DataServiceVersion: 1.0;
Location: http://andreiri/MyProject.Data/Data.svc/Zone(75)
Cache-Control: no-cache
Content-Type: application/atom+xml;charset=utf-8
Content-Length: 2213

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="http://andreiri/MyProject.Data/Data.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"&gt;
  <id>http://andreiri/MyProject.Data/Data.svc/Zone(75)&lt;/id&gt;
  <title type="text"></title>
  <updated>2009-09-11T13:36:47Z</updated>
  <author>
    <name />
  </author>
  <link rel="edit" title="Zone" href="Zone(75)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CenterZone" type="application/atom+xml;type=feed" title="CenterZone" href="Zone(75)/CenterZone" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ZoneMobil" type="application/atom+xml;type=feed" title="ZoneMobil" href="Zone(75)/ZoneMobil" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/StareZone" type="application/atom+xml;type=entry" title="StareZone" href="Zone(75)/StareZone" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/TipZone" type="application/atom+xml;type=entry" title="TipZone" href="Zone(75)/TipZone" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/User" type="application/atom+xml;type=entry" title="User" href="Zone(75)/User" />
    <category term="MyProject123Model.Zone" scheme="http://schemas.microsoft.com ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
    <m:properties>
      <d:IdZone m:type="Edm.Int32">75</d:IdZone>
      <d:X_Post m:type="Edm.Decimal">587647.4705</d:X_Post>
      <d:Y_Post m:type="Edm.Decimal">325783.077599999</d:Y_Post>
      <d:X_Repost m:type="Edm.Decimal" m:null="true" />
      <d:Y_Repost m:type="Edm.Decimal" m:null="true" />
      <d:Data m:type="Edm.DateTime">2009-09-11T16:36:40.588951+03:00</d:Data>
      <d:Detalii>aslkdfjasldkfj</d:Detalii>
      <d:IdElement m:type="Edm.Int32">1</d:IdElement>
    </m:properties>
    </content>
</entry>

Why do I get an exception? And, using this in a clean project does not throw the exception..

+1  A: 

Andrei,

Did you ever figure out why this is happening?

Corey Coto
Nope :( . I Just abandoned the problem, the employer, all :))
Andrei Rinea

related questions