views:

89

answers:

1

Hello, go easy on me, it's my first question :)

I've been working with linqToSql for about a month, and there is just this one thing that is bothering me...

Lets say I have an Entity Object called "Customer" and another called "CustomerType", Customer as a reference to CustomerType.

When inserting the Customer I need to set the CustumerType, I have the customerTypeID value that I want to set, so what I am doing is this:

if(c.CustomerTypeReference == null)
{
   c.CustomerTypeReference = new System.Data.Objects.DataClasses.EntityReference<CustomerType>
}


c.CustomerTypeReference.EntityKey = new System.Data.EntityKey("DataContext.CustomerType", "id", value);

This works but it seems to me over complicated. Is there any other way to do this?

Please take in mind that I do not want to get the object CustomerType from the database, this example is simple but I do work with objects that contain about 100 properties.

+1  A: 

Are you using the DBML Designer or have you hand-coded your entity classes? If you are working with the Designer, I would expect that since your table has a customerTypeID column, that there would be a property named customerTypeID in your Customer class. All you really need to do is set the value of this property to the value of the id

  Customer c = new Customer();
  c.customerTypeID = value;

This should work fine as long as you don't want to refer to the related entities themselves (i.e., you are just going to save this particular object, not use it right away). If you need to refer to the related entities -- say to access their properties -- it's going to have to go retrieve the values from the database. I'd have to look at the designer-generated code, but I think that the property setters on the id-value columns don't update the entity references, while the entity-reference setters do update the id-value fields.

tvanfosson
I am using the ADO.NET Entity Data Model. I do need to make references to the related objects, so the customerTypeID cannot be mapped(this code is just a simple example)...I would like to be able to do what you say, but at the same time keep the references to the other object.
Sergio