0

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?

Thr3e
  • 358
  • 1
  • 6
  • 22

2 Answers2

0

You should allow your database to allocate identities to your Foo records rather than trying to guess the identity to give. Almost an impossible task in a multi-user system.

When saving an entity using WCF RIA services you can get the identity allocated by:

riaContext.SubmitChanges ( (op) =>
  {
    if (!op.HasError)
    {
      var identityAdded = ((Entity) op.ChangeSet.FirstOrDefault()).GetIdentity();
    }
  }
Rus
  • 1,827
  • 1
  • 21
  • 32
  • thanks for the suggestion, but we have problem with custom starting numbers. e.g. my clients might just want to start from 1000, instead of 1. and also i am providing an option to just may be skip a series. e.g. the last "FooRef" is 34, they might just decide to skip until 100 and start next from 101. .... any more ideas? – Thr3e Jul 04 '12 at 01:18
  • Essentially you want FooRef to be unique. So I would create a unique constraint in the database to protect FooRef. Another route could be to use validation on your model. This would mean that if someone else had used FooRef of 100 and upon saving you would get a validation failure and could then manually resolve. – Rus Jul 04 '12 at 08:56
  • i tried having a unique constraint in the database but i already have a one using as primary key and i got database error while having two unique identifiers. – Thr3e Jul 10 '12 at 01:14
0

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.

Thr3e
  • 358
  • 1
  • 6
  • 22