Override FieldInfo.IsReadOnly property Question

Posts   
 
    
beekde
User
Posts: 14
Joined: 08-Jul-2005
# Posted on: 13-Nov-2007 21:14:51   

This is in relation to the thread http://llblgen.com/tinyforum/Messages.aspx?ThreadID=9003&HighLight=1 "Manually setting identity field "

By implementing the OnSaveEntity in the DataAccessAdapter I am able to set the identity insert on. My only problem at this point is figuring out how to change the IsReadOnly property in FieldInfo for my identity column. The designer does not allow me to make my identity column writable. Any suggestions on how I can either set the IsReadOnly property or override the generation of this on a per entity basis? Obviously the easiest solution is to implement the Set property in ORM but I'm wondering if there is another way without recompiling the ORM library.


#Region "Custom DataAccessAdapter code"
        
        ' __LLBLGENPRO_USER_CODE_REGION_START CustomDataAccessAdapterCode
        Protected Overrides Sub OnSaveEntity(ByVal saveQuery As SD.LLBLGen.Pro.ORMSupportClasses.IActionQuery, ByVal entityToSave As SD.LLBLGen.Pro.ORMSupportClasses.IEntity2)

            If entityToSave.LLBLGenProEntityName = "WarehouseEntity" Then
                Dim sqlcmdSetIdentityInsert As IDbCommand = saveQuery.Connection.CreateCommand
                If Not sqlcmdSetIdentityInsert.Connection.State = ConnectionState.Open Then sqlcmdSetIdentityInsert.Connection.Open()
                sqlcmdSetIdentityInsert.Transaction = Me.PhysicalTransaction
                sqlcmdSetIdentityInsert.CommandText = "SET IDENTITY_INSERT WHSE ON"
                sqlcmdSetIdentityInsert.ExecuteNonQuery()
            End If


            MyBase.OnSaveEntity(saveQuery, entityToSave)
        End Sub
        ' __LLBLGENPRO_USER_CODE_REGION_END
#End Region

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 14-Nov-2007 12:11:45   

use entity.Fields(index).ForcedCurrentValueWrite(value) to set a readonly field to a value. Then, you set its IsChanged flag to true to mark it as changed: entity.Fields(index).IsChanged = True

Frans Bouma | Lead developer LLBLGen Pro
beekde
User
Posts: 14
Joined: 08-Jul-2005
# Posted on: 15-Nov-2007 00:04:08   

Tried as you suggested


destEnt.Fields(Destination.WarehouseFieldIndex.WarehouseId).ForcedCurrentValueWrite(1)
destEnt.Fields(Destination.WarehouseFieldIndex.WarehouseId).IsChanged = True

but the generated insert query does not include the column and the sqlexception is Explicit value must be specified for identity column in table 'WHSE' either when IDENTITY_INSERT is set to ON or ........

If I manually change the readonly argument FROM


Private Sub InitWarehouseEntityInfos()
            MyBase.AddElementFieldInfo("WarehouseEntity", "WarehouseId", GetType(System.Int32), True, False, True, False, CInt(WarehouseFieldIndex.WarehouseId), 0, 0, 10)

TO


Private Sub InitWarehouseEntityInfos()
            MyBase.AddElementFieldInfo("WarehouseEntity", "WarehouseId", GetType(System.Int32), True, False, False, False, CInt(WarehouseFieldIndex.WarehouseId), 0, 0, 10)

it works. If the designer didn't disable the readonly flag on the properties I'd be golden. Is there another way?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 15-Nov-2007 11:30:45   

Oh, my fault flushed

It's indeed not possible in code. THe DQE checks if the field is an identity field and follows a different path. In v2.5 you can uncheck the IsIdentity flag in the designer, it shouldn't be greyed out. What's the DB type you're using and what's the llblgen pro version?

Frans Bouma | Lead developer LLBLGen Pro
beekde
User
Posts: 14
Joined: 08-Jul-2005
# Posted on: 15-Nov-2007 20:45:51   

Sql Server 2005 and I am using 2.5 (October 25). I have unchecked the identity for these entities. The problem is I can't uncheck the readonly. The readonly is what I believe is preventing the column from being included in the query.

I have another question as well. Is it possible to execute a dbfunction during insert. I see a simple example of setting the ExpressionToApply during entity Update. I see an example of calling a DBFunction during fetch. However, is it possible to execute a dbfunction during insert?

i.e


Dim destEnt As CategoryEntity = New CategoryEntity

destEnt.Fields(CInt(CategoryFieldIndex.PhysicalName)).ExpressionToApply = New DbFunctionCall("dbo.fn_UniquePhysicalName", New Object() {"A parameter", 3, Nothing})

adapter.SaveEntity(destEnt)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 20-Nov-2007 11:14:52   

beekde wrote:

Sql Server 2005 and I am using 2.5 (October 25). I have unchecked the identity for these entities. The problem is I can't uncheck the readonly. The readonly is what I believe is preventing the column from being included in the query.

Hmm, I see what you mean. Indeed, the readonly prevents it from being included. I think you've no other option than to override GetFieldPersistenceInfo(s) routines, so you replace the FieldInfo object for the identity field(s) in question with a new one which doesn't have readonly/identity set to true.

I have another question as well. Is it possible to execute a dbfunction during insert. I see a simple example of setting the ExpressionToApply during entity Update. I see an example of calling a DBFunction during fetch. However, is it possible to execute a dbfunction during insert?

i.e


Dim destEnt As CategoryEntity = New CategoryEntity

destEnt.Fields(CInt(CategoryFieldIndex.PhysicalName)).ExpressionToApply = New DbFunctionCall("dbo.fn_UniquePhysicalName", New Object() {"A parameter", 3, Nothing})

adapter.SaveEntity(destEnt)

Not at the moment, no.

Frans Bouma | Lead developer LLBLGen Pro