Print Page | Close Window

Insert into field same value as PK

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2366
Printed Date: 26-Mar-2025 at 5:45am


Topic: Insert into field same value as PK
Posted By: gkneo
Subject: Insert into field same value as PK
Date 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



Replies:
Posted By: sbelini
Date 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 http://drc.ideablade.com/xwiki/bin/view/Documentation/SecurityInterception - DevForce Resource Center .

In addition, EntityManager saves are transactional by default. You can find out more about transactions and DevForce in the http://drc.ideablade.com/xwiki/bin/view/Documentation/Saving_Transactions - DevForce Resource Center as well.

sbelini.


Posted By: gkneo
Date 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.


Posted By: sbelini
Date 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 http://drc.ideablade.com/xwiki/bin/view/Documentation/Code_MiniDemosSilverlight_BusObjPersistence - DevForce Resource Center  (you can find it at 030_BusObjPersistence\_MiniDemos\Silverlight\CodeCS\SilverlightConsole.sln among other solutions)
 
Silvio



Print Page | Close Window