This is a migrated thread and some comments may be shown as answers.

Object by reference and by Id

4 Answers 139 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Brandon
Top achievements
Rank 1
Brandon asked on 17 Feb 2009, 12:44 AM
I wish to reference other persistent objects both by value and by Id.  Yet the reverse-mapping wizard keeps undoing my changes in the app.config file.  I will try to explain what I mean below:

In my current project, I make all DB changes directly to my database using my own database administration tools.  Then, I use the reverse-engineering wizard to create my persistent classes.  My database has several foreign-key references.  For example, I have an OrderStatus table.  My Order table references the OrderStatus table via an OrderStatusId field.  In this case, the reverse-mapping wizard will output something along the lines of:

[Telerik.OpenAccess.Persistent] 
public partial class OrderStatus 
  private int id; 
  [Telerik.OpenAccess.FieldAlias("id")] 
  public int Id 
  { 
    get { return id; } 
    set { id = value; } 
  } 
 
  private string statusName; 
  [Telerik.OpenAccess.FieldAlias("statusName")] 
  public string StatusName 
  { 
    get { return statusName; } 
    set { statusName = value; } 
  } 
 
[Telerik.OpenAccess.Persistent] 
public partial class Order 
  private int id; 
  [Telerik.OpenAccess.FieldAlias("id")] 
  public int id 
  { 
    get { return id; } 
    set { id = value; } 
  } 
 
  private OrderStatus orderStatus; 
  [Telerik.OpenAccess.FieldAlias("orderStatus")] 
  public OrderStatus OrderStatus
  { 
    get { return orderStatus; } 
    set { orderStatus = value; }  
  } 

Obviously, the reverse-mapping wizard places the private field entries in a code-behind file, so the output would consist of two files for each class.

Now, because I also wish to access the OrderStatusId field directly (rather than through the referenced object), I have added the following code to the main class declaration (not to the code-behind file):

public partial class Order 
  ... 
  private int orderStatusId; 
  [Telerik.OpenAccess.FieldAlias("orderStatusId")] 
  public int OrderStatusId  
  { 
    get { return orderStatusId; } 
    set { orderStatusId = value; }  
  } 

Now, my class contains both an object reference and an Id reference to the OrderStatus.  At this point I run the forward-mapping wizard and select the orderStatusId field.  By default, the wizard has chosen "order_status_id" as the field name.  However, my field name is "OrderStatusId," so I change it.  At this point, I compile my project and everything works fine.  Then, the next time I run the reverse-mapping wizard,  OpenAccess has updated my app.config file and undone my change ("order_status_id" to "OrderStatusId").  It now has the field name listed as "order_status_id" again.

How can I cause either:
1. OpenAccess to automatically generate both an object AND an Id reference?
2. Stop undoing my changes to the app.config file?

Let me know if you require any further clarification.

4 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 19 Feb 2009, 08:41 AM
Hello Brandon,

It is not possible to generate both the Id and the reference field with OpenAccess yet. The reason for that behavior is to prevent the user from assigning one value to the "OrderStatusId"  field and another to the reference "OrderStatus" filed which has a different "Id" for itself.

It is not necessary to implement additional property which should point to the foreign key field because to OpenAccess it is just another "int" column and when loading data from your database there is no way for OpanAccess to know that it should match the primary key of another table.

If you want to have access to the foreign key field of your table you could write a wrapper property which would have the following structure:
public int OrderStatusId     
  {    
    get { return this.orderStatus.Id; }    
    set { this.orderStatus.Id = value; }     
  }    
 

I hope my response helps you in working with OpenAccess more successfully. For any further requests or suggestions please do not hesitate to contact us.

Sincerely yours,
Zoran
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Brandon
Top achievements
Rank 1
answered on 19 Feb 2009, 02:18 PM
Such a property seems to crash my application because 'orderStatus.Id' will point to a null object in the case of a new record.  I tried working around this by:
public int OrderStatusId  
{  
  get   
  {  
    if (OrderStatus == null)  
      return 0;  
    else   
      return OrderStatus.Id;   
  }  
  
  set  
  {  
    QueryResultList<OrderStatus> qry = ObjectScope.GetOqlQuery<OrderStatus>(  
      "select * from OrderStatusExtent where Id = " + value.toString()).Execute();  
    if (qry.Count > 0)  
      OrderStatus = qry[0];  
  }  
}  

The problem with this is there are many foreign keys in my database and this much extra coding is making the use of OpenAccess less efficient than doing the same thing with standard SqlDataSource components.
0
Accepted
Thomas
Telerik team
answered on 20 Feb 2009, 05:12 PM
Hi Brandon,

is the OrderStatus changeable as object? Or is there a fixed amount where 1 means delivered and 2 in_processing, etc? Then you should use a calculating property which just returns the OrderStatus object by issuing a GetObjectById().
If there is a 1:1 relationship between Order and OrderStatus, why don't you just create an OrderStatus instance immediately and set that reference to the new Order? In this case, the OrderStatus is even dependend on the Order (a status without referencing Order does not make sense). You could then avoid the OQL queries completely, and could even declare the OrderStatus field as part of the default fetch group as it is certainly required when fetching the Order.

Greetings,
Thomas
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Brandon
Top achievements
Rank 1
answered on 21 Feb 2009, 04:57 PM
It is the second option, a fixed amount where 1 = delivered, 2 = processing, etc.  Thanks for your help.
Tags
General Discussions
Asked by
Brandon
Top achievements
Rank 1
Answers by
Zoran
Telerik team
Brandon
Top achievements
Rank 1
Thomas
Telerik team
Share this question
or