SetNewFieldValue and related entities

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 29-Jun-2005 02:52:02   

I am trying to use the following code to create two related entities and keep getting the error "object reference not set to an instance of an object". The first field in the loop is Person.FirstName which I have mapped to the field Employee.FirstName.

I assume that this error is thrown because there isn't an actual field for Employee.FirstName. Is there anyway around this?


EmployeeEntity employee = new EmployeeEntity();
employee.Person = new PersonEntity();

foreach(GridEXCell cell in e.Row.Cells)
{
        if ( cell.DataChanged )
    employee.SetNewFieldValue( cell.Column.DataMember, cell.Value );
}
ServiceFactory.GetPersistanceManager().SaveEntity(employee);

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 29-Jun-2005 12:48:35   

Employee.FirstName is a Field Mapped on related field I pressume? These aren't defined in the fields object, so you can't set these using SetNewFieldValue() as they're not really part of the entity.

You can work around this as follows:


EmployeeEntity employee = new EmployeeEntity();
employee.Person = new PersonEntity();

foreach(GridEXCell cell in e.Row.Cells)
{
    if ( cell.DataChanged )
    {
        string fieldName = cell.Column.DataMember;
        if(Enum.IsDefined(typeof(EmployeeFieldIndex), fieldName))
        {
            employee.SetNewFieldValue(fieldName, cell.Value );
        }
        else
        {
            employee.Person.SetNewFieldValue(fieldName, cell.Value);
        }
    }
}

Another is via property descriptors but that's really awkward.

I'll try to fix this in the upcoming upgrade with an override of SetNewFieldValue, which simply does this.Name = value, no matter what Name is: a field, a field mapped onto a related field or a field mapped onto an m:1/1:1 relation.

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 29-Jun-2005 19:07:02   

Thanks for the answer Otis. Isn't this problem something that the new table inheritance feature will solve. I assume the table inheritance feature will allow me to merge multiple tables into a single entity which is what I am trying to do here.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 30-Jun-2005 10:57:22   

tprohas wrote:

Thanks for the answer Otis. Isn't this problem something that the new table inheritance feature will solve. I assume the table inheritance feature will allow me to merge multiple tables into a single entity which is what I am trying to do here.

Yes, that will fix this issue for you. You then simply use an employee entity, and the person fields are part of it.

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 15-Mar-2006 23:57:40   

tprohas wrote:

Thanks for the answer Otis. Isn't this problem something that the new table inheritance feature will solve. I assume the table inheritance feature will allow me to merge multiple tables into a single entity which is what I am trying to do here.

Well, I'm stuck with this problem again. I know that using inheritance would solve part of my problem, but I now need to make SetNewFieldValue work with "Fields mapped on related fields". Will this ever be possible?

Another side of this problem that I keep running into is that the entity.Fields collection only contains the fields from the entity and I would like the collection to contain the "Fields mapped on related fields". Maybe there could be a different fields collection that would do this. Could this be possible?

Thanks for the great product!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 16-Mar-2006 11:48:16   

tprohas wrote:

tprohas wrote:

Thanks for the answer Otis. Isn't this problem something that the new table inheritance feature will solve. I assume the table inheritance feature will allow me to merge multiple tables into a single entity which is what I am trying to do here.

Well, I'm stuck with this problem again. I know that using inheritance would solve part of my problem, but I now need to make SetNewFieldValue work with "Fields mapped on related fields". Will this ever be possible?

fields mapped onto a related field aren't physically part of the entity, so they're not part of the routine which sets fields inside its own entity nor are they part of the entityfields object.

What you want is more that you want to map an entity onto multiple tables without using inheritance, as that's the result, is that correct?

could you elaborate a bit why you won't go / can't go for the entity.relatedentity.field approach?

Frans Bouma | Lead developer LLBLGen Pro