Print Page | Close Window

Setting Primay Key values

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1588
Printed Date: 13-Mar-2025 at 10:06am


Topic: Setting Primay Key values
Posted By: downeytim
Subject: Setting Primay Key values
Date Posted: 15-Dec-2009 at 11:26am
The object mapper by default sets all PK fields to ReadOnly.
 
In many cases in our application we are passing the values for the PK to the create method on a Business Object class.
 
In the example below ComponentId is the PK of IOComponentOption.  How do I set this without changing all the field definitions in the Object Mapper for 500+ business objects?  I assume I can use the SetColumnValue method but this is a lot of code to rewrite for almost every business object.
 
EXAMPLE:
 

public static IOComponentOption Create(int componentId){

   IOComponentOption option = Session.PM.CreateEntity<IOComponentOption>() as IOComponentOption;

   option.ComponentId = componentId;

   option.Data = String.Empty;

   option.AddToManager();

   return(option);

}




Replies:
Posted By: davidklitzke
Date Posted: 15-Dec-2009 at 1:59pm

I am not completely sure what you are trying to do, but you may find this trick helpful.  Assume emp is an Employe

emp.id = 22
 
gives compiler error.  "Property Id is read-only";
 
but
 
emp["Id"] = 22;
 
compiles and assigns 22 to Id property;
 
You can also use:
 
emp[0] = 22;



Print Page | Close Window