New Posts New Posts RSS Feed: Insert into field same value as PK
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Insert into field same value as PK

 Post Reply Post Reply
Author
gkneo View Drop Down
Newbie
Newbie
Avatar

Joined: 23-Jun-2010
Posts: 21
Post Options Post Options   Quote gkneo Quote  Post ReplyReply Direct Link To This Post Topic: Insert into field same value as PK
    Posted: 10-Dec-2010 at 2:41am
Hi.  

I have a table with 2 fields:

Id: int pk
CheckField: nvarchar(20)
ControlField: nchar(1)


I have created my devforce entity A from this table.
Now, I want to create and insert  a list of entities of type A.  I want to store in CheckField the same value from Id, but sometimes (depends upon some business logic), I want to store any value.  So, I would have something like this:
Id      CheckField      ControlField
1      1                  A
2      2                  A
3      0000XYZ            B
4      4                  A
5      5                  A
6      0000FGH            B

Is it possible to achieve using a SaveChangesAsync?  I want to do this in a single transaction, and cannot use stored procedures (architectural decision)

Guillermo
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 13-Dec-2010 at 10:09am
Hi Guillermo,

You could do your checking once you call SaveChangesAsync by implementing the EntityServerSaveInterceptor.
You can override the ExecuteSave method, and modify the value of CheckField before calling the base implementation of the ExecuteSave method:

public class EntityServerSaveManager : EntityServerSaveInterceptor {
    protected override bool ExecuteSave() {
        var em = EntityManager;
        var savedEntities = em.FindEntities(EntityState.Added).OfType<MyEntityType>();
        foreach (MyEntityType myEntity in savedEntities) {
            if (any criteria deciding what goes on CheckField) {
                myEntity.CheckField = myEntity.Id;
            } else {
                myEntity.CheckField = something_else;
            }
        }
        return base.ExecuteSave();
    }
}


You can find detailed information about Save Interception in our DevForce Resource Center.

In addition, EntityManager saves are transactional by default. You can find out more about transactions and DevForce in the DevForce Resource Center as well.

sbelini.
Back to Top
gkneo View Drop Down
Newbie
Newbie
Avatar

Joined: 23-Jun-2010
Posts: 21
Post Options Post Options   Quote gkneo Quote  Post ReplyReply Direct Link To This Post Posted: 13-Dec-2010 at 11:54pm
Hi sbelini,

I forgot to mention that Id field is an identity field, so  the line 
 myEntity.CheckField = myEntity.Id;
will store "-101" in CheckField.  

This was the first thing I tried, but I am sorry I didn't mention it in my original post.


Edited by gkneo - 13-Dec-2010 at 11:57pm
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 05-Jan-2011 at 4:36pm

Guillermo,

the other alternative I found is to implement a custom IIdGenerator, assign the IDs using
mgr.GenerateId(myTestEntity, TestEntity.PropertyMetadata.Id);
and call
mgr.ForceIdFixup();
right before calling the save.
This way you can implement your checking in the SaveInterceptor.
 
We have a simple custom IIdGenerator implemented in our samples in our DevForce Resource Center (you can find it at 030_BusObjPersistence\_MiniDemos\Silverlight\CodeCS\SilverlightConsole.sln among other solutions)
 
Silvio
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down