Hi Walaa,
Here is my unit test:
[TestMethod]
public void InsertWithExpression()
{
using (DataAccessAdapter adapter = new DataAccessAdapter(false))
{
// create new entity
GcLockSessionEntity newEntity = new GcLockSessionEntity(Guid.NewGuid())
{
LockedByUserId = Guid.NewGuid()
};
// set field value to expression
newEntity.Fields[(int)GcLockSessionFieldIndex.LockExpiresTime].ExpressionToApply =
new DbFunctionCall("DATEADD(mi,{0},getdate())", new object[] { 30 });
newEntity.Fields[(int)GcLockSessionFieldIndex.LockExpiresTime].IsChanged = true;
// insert new entity
adapter.SaveEntity(newEntity);
}
}
Which results in the following sql insert statment. You can see that the value for @LockExpiresTime is undefined, even though we set the expression to apply for that field.
Query: INSERT INTO [Northwind].[dbo].[gc_LockSession] ([LockSessionId], [LockedByUserId], [LockExpiresTime]) VALUES (@LockSessionId, @LockedByUserId, @LockExpiresTime)
Parameter: @LockSessionId : Guid. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 0a9ea093-c5dd-4e9e-b820-74a060591190.
Parameter: @LockedByUserId : Guid. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 90b53189-79dd-4712-bc67-c09643be01c2.
Parameter: @LockExpiresTime : DateTime. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.
On the database, that field is non nullable, so I get a sql exception when this code runs.
If I really want to set the expression, I have to save the entity twice: once as in insert with a placeholder value, and once as an update with the dbFunction:
[TestMethod]
public void UpdateWithExpression()
{
using (DataAccessAdapter adapter = new DataAccessAdapter(true))
{
// create new entity
GcLockSessionEntity newEntity = new GcLockSessionEntity(Guid.NewGuid())
{
LockExpiresTime = DateTime.Parse("1/1/1900"),// temp value
LockedByUserId = Guid.NewGuid()
};
// perform insert:
adapter.OpenConnection();
adapter.SaveEntity(newEntity, true);
// set field value to expression:
newEntity.Fields[(int)GcLockSessionFieldIndex.LockExpiresTime].ExpressionToApply = new DbFunctionCall("DATEADD(mi,{0},getdate())", new object[] { 30 });
// tell entity that it needs to update the db
newEntity.Fields[(int)GcLockSessionFieldIndex.LockExpiresTime].IsChanged = true;
newEntity.IsDirty = true;
// perform update
adapter.SaveEntity(newEntity);
adapter.CloseConnection();
}
}
Because I set the placeholder value, SqlServer doesn't complain. The LLBLGen runtime is able to set the expression on update, and this is the generated update command:
Query: UPDATE [Northwind].[dbo].[gc_LockSession] SET [LockExpiresTime]=DATEADD(mi,@LO1e1,getdate()) WHERE ( [Northwind].[dbo].[gc_LockSession].[LockSessionId] = @LockSessionId2)
Parameter: @LO1e1 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 30.
Parameter: @LockSessionId2 : Guid. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 0e338b77-ccd7-4b1f-a13f-acfbb0490c29.
Now, of course we could perform 2 commands for each inserted entity, but I was wondering out of curiosity
, how much work it would be to modify the DataAccessAdapter in order to call my expression on insert.