System.InvalidOperationException: The context is not currently tracking the entity.
- Racking my brain, please help!
I am trying to add a record using ADO.NET Dataservices. Here is my database and code.
table TB_WEB_PROP
column PROP_ID (number) identity
column PROP_NAME (varchar(255))
table TB_WEB_ALERT
column ALERT_ID (number) idenity
column PROP_ID_REL (number) FK is TB_WEB_PROP.PROP_ID
I built an ADO.NET DataModel and exposed using DataServices on the Gateway. The Navigation properties are showing correctly in the Model. I can query the TB_WEB_ALERT object with keys sucessfully. The client is trying to add a record in the TB_WEB_ALERT table, but I cannot get the Foreign Key to work no matter what I try. I have manyc errors like Context Tracking or Null Values in Second, etc.
Here is hte CODE I am using for the update. I suspect it is all in here:, my tables above are condensed versions of the real thing.
public void InsertAlertRecord(GuestCardObject guestCard)
{
TB_WEB_ALERT alert = new TB_WEB_ALERT();try
{
service = new RESTPropertyEntities(new Uri(configService));var propQuery = from TB_WEB_PROP prop in service.TB_WEB_PROP//.Expand("TB_WEB_ALERT")
where prop.PROP_ID == guestCard.PropId
select prop;alert.BATH = guestCard.Bath;
alert.BED = guestCard.Bed;
alert.CREATE_BY = "ALERTFORM";
alert.UPDATE_BY = "ALERTFORM";
alert.DATE_FROM = guestCard.DateFrom;
alert.DATE_TO = guestCard.DateTo;
alert.EMAIL = guestCard.Email;
alert.ENABLED_FLAG = true.ToString();
alert.FIRSTNAME = guestCard.FirstName;
alert.LASTNAME = guestCard.LastName;
alert.PHONE = guestCard.PhoneNumber;
alert.PRICE_FROM = guestCard.PriceFrom;
alert.PRICE_TO = guestCard.PriceTo;
alert.TERM = decimal.Parse(guestCard.Term);
alert.TB_WEB_PROP = new TB_WEB_PROP();
alert.TB_WEB_PROP.PROP_ID = propQuery.FirstOrDefault().PROP_ID;
service.MergeOption = System.Data.Services.Client.MergeOption.AppendOnly;try
{
service.AddToTB_WEB_ALERT(alert);
service.AddLink(service.TB_WEB_PROP, "PROP_ID", alert.TB_WEB_PROP);
service.SaveChanges();}
catch (Exception ex)
{
throw ex;
}
}
catch (Exception ex)
{
LogWriter.Log(2, "Exception Inserting Alert Record", ex);
}}
Answers
- If your entity has any navigational properties
in this case its
alert.TB_WEB_PROP = new TB_WEB_PROP();
then you have to use AttachTo() and set link method to notify data context abt this relationship
like
service.AttachTo("TB_WEB_PROP ", alert.TB_WEB_PROP );
service.SetLink(alert , "TB_WEB_PROP ", alert.TB_WEB_PROP );
before SaveChanges()
Let me know if this helps.- Marked As Answer byJeremy Schaab Wednesday, November 25, 2009 5:27 PM
All Replies
- If your entity has any navigational properties
in this case its
alert.TB_WEB_PROP = new TB_WEB_PROP();
then you have to use AttachTo() and set link method to notify data context abt this relationship
like
service.AttachTo("TB_WEB_PROP ", alert.TB_WEB_PROP );
service.SetLink(alert , "TB_WEB_PROP ", alert.TB_WEB_PROP );
before SaveChanges()
Let me know if this helps.- Marked As Answer byJeremy Schaab Wednesday, November 25, 2009 5:27 PM
- THANK YOU, saved me hours of wrapping my mind into nothing! I appreciate your help.
- Hi,
Maybe a bit better (faster) solution would be to not create a new TB_WEB_PROP() since you want to actually use the one returend from the query.
So something like this:
alert.TB_WEB_PROP = propQuery.FirstOrDefault();
And then just the Add and SetLink should work.
Thanks,
Vitek Karas [MSFT]


