I am using RIA Services with Entity Framework in Silverlight 5. i have a following Foo
table;
Fooid
FooName
FooCode
FooRef
Fooid
is an auto-increment number - works like a charm on save ...
FooName
is set in ViewModel - works fine on save ...
FooCode
is a 3 letter code e.g. GRN, SIP, XYZ ... this is also set in the ViewModel .. no problem
FooRef
is an incrementing integer. e.g. last saved value on server is 100, now the next save should be 101.
i am a bit confused as how to get this latest number. i researched a bit and i found out two ways but i am confused using these and needs help how to implement.
First Method
from ViewModel, i can call an INVOKE from RIA Service, which can provide me the last saved FooRef
int. i can add it by 1 and pass the value on SubmitChanges.
Problem with First Method :
The problem i face with this method is, lets say there are 3 users at their workstations at different locations. they all start creating a new Foo
, when they call invoke method, they all will be given the same value which kind of breaks the whole concept down.
Second Method
I somehow intercept the Add method on server, may be via Repository, and get the value of FooRef
just before Adding this entity and submitting changes.
Onyone can help?
At the end, i just went on with Invoke method, and in the invoke callback function, i end up submitting the context to server.
context.GetAssignedFooRef((op =>
{
entityNewFoo.FooRef = op.Value; // assign return int to FooRef
context.Foo.Add(entityNewFoo);
context.SubmitChanges();
}
This is how i tackle my problem, i dont know if it is an ideal solution or even close to it, but it works with me. The only problem i face is that, if there is another user seeking a FooRef from invoke before the first one saved. This is where Validation kicks in, and there i am going to request a new FooRef if there exists a same FooRef in the database.