Expressions in Entity Updates

Posts   
 
    
KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 14-Sep-2007 13:18:41   

v2.0 / Adapter

Hi,

For a specific field of an entity I want to apply a specific funcion when updating it. This is actually possible with the following code (taken from the manual):


EmployeeEntity employee = new EmployeeEntity();
employee.Fields[(int)EmployeeFieldIndex.Salary].ExpressionToApply = new DbFunctionCall(...);
adapter.UpdateEntitiesDirectly(employee, null);

Is it somehow possible to pass that function call when saving an entity instance. Something like this:


adapter.SaveEntity(myEntity, myFieldExpression_on_a_specific_field);

Background: This post is actually related to one of my former questions about data de/encryption. One field should be decrypted when reading it and encrypted when writing it by using a function of SQL Server. To read the entity I implemented a custom factory that does the decryption. That works good. But there is no straight forward way to write the data back to the db. With UpdateEntitiesDirectly it works but I'm wondering if there is an easier way to do that.. Something like a reusable object that can be passed to all SaveEntity calls. Similar like I can pass a custom factory when fetching objects.

Thanks for all hints! sunglasses

Cheers.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Sep-2007 16:17:12   

Please try the following:

myEntity.Fields[(int)EmployeeFieldIndex.Salary].ExpressionToApply = new DbFunctionCall(...);
myEntity.Fields[(int)EmployeeFieldIndex.Salary].IsChanged = true;
myEntity.IsDirty = true;
adapter.SaveEntity(myEntity)
KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 14-Sep-2007 16:54:31   

Thanks! That works fine smile

But now I am having another problem. When loading an entity via my custom factory I can't update the entity anymore. In the factory I change the number of fields of the entity and I guess I can't save the entity anymore after this change. Is there a solution to resolve that?

My factory


public class MyFactory:EmployeeSalaryEntityFactory {
        private string mDecryptionPassword = "";

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="decryptionPassword">Password used to decrypt data</param>
        public EmployeeSalaryEntityFactoryWithSalaryDecryption(string decryptionPassword):base() {
            mDecryptionPassword = decryptionPassword;
        }

        /// <summary>
        /// Define new fields
        /// </summary>
        /// <returns></returns>
        public override IEntityFields2 CreateFields() {
            IEntityFields2 toReturn = base.CreateFields();
            toReturn.Expand(1); // add field

            // define function call for decryption
            IExpression netSalaryDecryption = new DbFunctionCall("DecryptByPassPhrase", new object[] { mDecryptionPassword, EmployeeSalaryFields.EncryptedNetSalary });

            // define new field
            IEntityField2 netSalaryField = new EntityField2("DecryptedNetSalary", netSalaryDecryption);

            // add field to collection
            toReturn.DefineField(netSalaryField, toReturn.Count - 1);

            return toReturn;
        }
    }

Exception that is thrown when loading an entity with this factory and saving it via adapter.SaveEntity(..)

at SD.LLBLGen.Pro.ORMSupportClasses.DynamicQueryEngineBase.ConstructFieldsToUpdateList(IEntityFieldCore[] fields, IFieldPersistenceInfo[] fieldsPersistenceInfo, List1& fieldsToUpdate, List1& persistenceInfoFieldsToUpdate)\r\n at SD.LLBLGen.Pro.DQE.SqlServer.DynamicQueryEngine.CreateSingleTargetUpdateDQ(IEntityFieldCore[] fields, IFieldPersistenceInfo[] fieldsPersistenceInfo, IDbConnection connectionToUse, IPredicate updateFilter)\r\n at SD.LLBLGen.Pro.ORMSupportClasses.DynamicQueryEngineBase.CreateUpdateDQ(IEntityFieldCore[] fields, IFieldPersistenceInfo[] fieldsPersistenceInfo, IDbConnection connectionToUse, List1 pkFilters)\r\n at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.CreateUpdateDQ(IEntity2 entityToSave, IFieldPersistenceInfo[] persistenceInfoObjects, List1 pkFilters)\r\n at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions)\r\n at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, IPredicateExpression updateRestriction, Boolean recurse)\r\n at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave)\r\n

I guess I am not the first one who is facing that problem.. wink However, I wasn't able to find a solution in this forum or in the manual.. disappointed

Thanks for your help!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Sep-2007 17:32:09   

Yes you are not the only one who faced this problem. (ref: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=10287)

Entities created by a Custom Factory that expands the Entity Fields can not be saved.

KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 17-Sep-2007 08:22:07   

ok.. that's a pity! disappointed

Anyway, thanks for your help!