SetProperty inside DataPortal_Insert Fails

SetProperty inside DataPortal_Insert Fails

Old forum URL: forums.lhotka.net/forums/t/6594.aspx


misogsk posted on Wednesday, March 11, 2009

Hi All,

I'm indicating a very strange problem. I have property named IDProperty, with default value set to 0. I want to insert a new object to database using LINQ. Then I need to save a new ID retrieved from LINQ object to IDProperty to use it later.

Problem is that IDProperty is still set to 0 even when I used SetProperty(IDProperty, order.ID);

Can someone help me, please?

Thanks in advance

 

Here is my code:

//Property declaration

private static PropertyInfo<int> IDProperty =

RegisterProperty(typeof(BusinessUnit), new PropertyInfo<int>("ID"));

public int ID

{

get { return GetProperty(IDProperty); }

set { SetProperty(IDProperty, value); }

}

//Data portal

[Transactional(TransactionalTypes.TransactionScope)]

protected override void DataPortal_Insert()

{

using (var ctx = ContextManager<Arrow.BusinessObjects.Core.Dbml.ArrowDBDataContext>.GetManager("ArrowDB", true))

{

Arrow.BusinessObjects.Core.Dbml.BusinessUnit obj = new Arrow.BusinessObjects.Core.Dbml.BusinessUnit();

obj.Name = GetProperty(NameProperty);

ctx.DataContext.BusinessUnits.InsertOnSubmit(obj);

ctx.DataContext.SubmitChanges();

this.SetProperty(IDProperty, obj.ID);

}

}

 

ajj3085 replied on Wednesday, March 11, 2009

There doesn't seem to be any problem with the code you've posted, so the issue is likely somewhere else.

misogsk replied on Wednesday, March 11, 2009

Should the problem be in core of CSLA.NET or do I need some special options for properties?

RockfordLhotka replied on Wednesday, March 11, 2009

Two things to consider.

First, you should typically use LoadProperty() and ReadProperty() in your data portal methods, not SetProperty() and GetProperty().

Second, when you call Save() on the client, are you using the result of that call?

_cust = _cust.Save()

As I discuss in chapters 1 and 2 of the book, the Save() method returns a new object, and if you don't use that new object then you'll never see the changes made to your data in the DataPortal_XYZ methods.

misogsk replied on Wednesday, March 11, 2009

Thank you very much. I appreciate your help. I forgot to save the result object.

Copyright (c) Marimer LLC