Problem with save

Posts   
 
    
Posts: 26
Joined: 23-May-2005
# Posted on: 24-Apr-2006 14:25:17   

I have an entity named: Organisation

if i update a record from a form like this is works ok

Dim organisation as OrganisationEntity : Some code to read the record : Organisation.Name = 'my organisation' Organisation.Save

But when i use this

Dim Entity As EntityBase

Me.Entity.Fields("Name").CurrentValue = 'my organisation' Me.Entity.Save

What's the difference. I also tried Dim Entity as IEntity with no luck

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 24-Apr-2006 15:04:21   

Although you didn't post a complete code snippet, like how do you retrieve the entity you want when you define it as EntityBase?

It looks like a casting problem, you should cast the entity to the type you need to retreive and update.

Posts: 26
Joined: 23-May-2005
# Posted on: 24-Apr-2006 18:39:22   

This is the full code i used to test the case

Imports SD.LLBLGen.Pro.ORMSupportClasses Imports MyDAL.EntityClasses

Partial Class _Default Inherits System.Web.UI.Page

Dim organisation As OrganisationEntity

Dim _entity As EntityBase

Public Property Entity() As EntityBase Get Return _entity End Get Set(ByVal value As EntityBase) _entity = value End Set End Property

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

Me.Entity = OrganisationEntity.GetByKey(2)
TextBox1.Text = Me.Entity.Fields("Name").DbValue

'organisation = OrganisationEntity.GetByKey(2)
'TextBox1.Text = organisation.Name

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ok As Boolean Me.Entity.Fields("Name").CurrentValue = TextBox1.Text Me.Entity.Save()

'this code works
'organisation.Name = TextBox1.Text
'organisation.Save()

End Sub End Class

Posts: 26
Joined: 23-May-2005
# Posted on: 24-Apr-2006 19:28:03   

Did some further testing, using your hint to cast the entity

guess that; if i change the modification of the field from

Entity.Fields("Name").CurrentValue = TextBox1.Text
Entity.Save()
      to

CType(Entity, OrganisationEntity).Name = TextBox1.Text
Entity.Save()

it does work.

Sorry but i don't see the difference between the two statements

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 24-Apr-2006 23:06:25   

Hi John,

What if you set the entity to dirty (entity.IsDirty = true) and then persist it (save), doesn't that work ?

Greetings, Dan

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 25-Apr-2006 01:45:23   

John wrote:

Did some further testing, using your hint to cast the entity

guess that; if i change the modification of the field from

Entity.Fields("Name").CurrentValue = TextBox1.Text
Entity.Save()
      to

CType(Entity, OrganisationEntity).Name = TextBox1.Text
Entity.Save()

it does work.

Sorry but i don't see the difference between the two statements

The first one gives no indication as to what table the entity persisted should be saved.

The second gives the ORM engine the info it needs to construct the insert/update SQL statement.

You should also be able to use CurrentValue once you have cast the entity.


    CType(Entity, OrganisationEntity).CurrentValue = TextBox1.Text

Posts: 26
Joined: 23-May-2005
# Posted on: 25-Apr-2006 22:18:20   

DvK wrote:

Hi John,

What if you set the entity to dirty (entity.IsDirty = true) and then persist it (save), doesn't that work ?

Greetings, Dan

Thanks Dan that did the trick. I was aware of the flag but i thought is was readonly. Still wonder why the flag is not set when i change the field through the baseclass.

Posts: 26
Joined: 23-May-2005
# Posted on: 25-Apr-2006 22:20:58   

JimHugh wrote:

John wrote:

Did some further testing, using your hint to cast the entity

guess that; if i change the modification of the field from

Entity.Fields("Name").CurrentValue = TextBox1.Text
Entity.Save()
      to

CType(Entity, OrganisationEntity).Name = TextBox1.Text
Entity.Save()

it does work.

Sorry but i don't see the difference between the two statements

The first one gives no indication as to what table the entity persisted should be saved.

The second gives the ORM engine the info it needs to construct the insert/update SQL statement.

You should also be able to use CurrentValue once you have cast the entity.


    CType(Entity, OrganisationEntity).CurrentValue = TextBox1.Text

Jim,

I fill the entity in the code at the beginning. The entity is the correct type. But as Dan suggested the dirty flag isnt changed by the setting of the currentvalue. Am i doing something wrong?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Apr-2006 09:56:52   

Use entity.SetNewFieldValue(name, value) or entity.SetNewFieldValue(index, value) instead. Don't set field values via CurrentValue directly if you can avoid it: setting field values in a generic method is to be done via SetNewFieldValue, which is a method of every entity.

Frans Bouma | Lead developer LLBLGen Pro