Reputation: 9985
First of all I wanted to thank you all for your effort and input. Stack Overflow has been the go-to resource for me, I feel that I've learned more in this forum than the entire 4 years at my college!
So onto the question. I have a Silverlight 4 project using EF and WCF RIA.
In one of my pages I retrieve the contents of a table via a Ria web service call and store the result in an ObservableCollection<T>
SILVERLIGHT CODE:
//class variables;
public ObservableCollection<Data> DataSource { get; set; } //Data entity
public ApplicationDomainContext Context { get; set; } //Ria Service
...
EntityQuery<Data> query = this.Context.GetDatasQuery();
this.Context.Load(query, loadedCallBack =>
{
if( loadedCallBack.HasError )
{
loadedCallBack.MarkErrorAsHandled();
MessageBox.Show("Unable to retrieve the desired data...");
return;
}
this.DataSource = new ObservableCollection<Data>(loadedCallback.Entities);
}
...
private void CreateUserAction()
{
string userName = WebContext.Current.User.Name;
this.Context.CreateUserAction(userName, this.DataSource, callBack =>
{
if(callBack.HasError)
{
callBack.MarkErrorAsHandled();
MessageBox.Show("Error creating user action");
return;
}
}
}
SERVICE CODE:
public partial class ApplicationDomainService : LinqToEntitiesDomainService<ApplicationDomainModel>
{
[Invoke]
public void CreateUserAction(string userName, IEnumerable<Data> dataItems)
{
foreach(Data dataItem in dataItems)
{
if( dataItem.EntityState == EntityState.Detached )
{
this.ObjectContext.Attach(dataItem); //ERROR???
}
}
}
}
So the code executes to the inside the if( dataItem.EntityState == EntityState.Detached)
and gives me an error when I try to attach the object:
An object with a null EntityKey value cannot be attached to an object context.
What's funny is that I do not modify the Data objects at all, and they do arrive with an Id and everything else on the client side.
If anyone can point me in the direction of what I am doing wrong I would greatly appreciate it!!!
Thanks
Martin, aka <bleepzter/>
Upvotes: 0
Views: 487
Reputation: 53
I haven't used the latest version of RIA and EF yet, but you might want to try the following changes:
Change this:
this.ObjectContext.Attach(dataItem);
To this:
this.ObjectContext.Data.AttachAsModified(dataItem)
Data may be pluralized on the ObjectContext depending on your settings.
Upvotes: 2