问题
In my silverlight project, I am using RIA DomainDataService to get objects from the asp.net project.
public class MyObject
{
[Key]
public int Id{get;set;}
public double XValue {get;set;}
}
This is the static object in the RIA Service - its scope is class level.
private static List<MyObject> Models = new List<MyObject>();
In asp.net project, I insert objects in Models and get it on silverlight end. Later on, I have to modify objects in the static List but the List always returns the same objects and not the modified versions of the object.
I modify the object such as:
Models[0].XValue = 0.1;
On the RIA Service, I can see the list being modified. The service method then returns this object but silverlight gets the same old List and not the modified one.
It seems like RIA Service keeps the objects in memory and just returns the same object.
How do I edit my custom class "MyObject"'s properties values such that Silverlight front end can get the modified list? Do I need to make MyObject editible? Please guide.
Found the solution. Answered below incase if i ever helps anyone :-)
回答1:
RIA is built to use a database to store the objects -- if you aren't using a database, you can use a standard WCF service and return your List<> through that.
回答2:
RIA service will not modify an object on the client side if an object's Key property is the same. Even if we modify other properties of an object, it will not be picked up. If we are sending a List from RIA service to client side Silverlight application then we will have to set a new value for the Key property of the object after we modify it so it is refreshed and silverlight can get the updated changes.
Therefore,
Create a public Guid UniqueID property in your XYZ class
Add [Key] attribute to it
When creating an object set its
value to Guid.NewGuid()
When updating an object, set its
value to Guid.NewValue()
If you want to update all of the Guid values of a list then you can use anonymous method:
MyList.Select(c => { c.UniqueID = Guid.NewGuid(); return c; }).ToList();
Hope it helps
来源:https://stackoverflow.com/questions/6508113/ria-service-class-objects-are-not-updated