views:

72

answers:

1

Hi, I have 2 entities: User and Company, with a FK from the User to the Company.

I'm trying to remove the association and leave the user entity with a scalar property "CompanyId", but still have the "Company" entity in the model (mainly to increase performance, I don't need to full entity attached to it).

I'm able to achieve that only by removing the association and then go to the edmx (xml) file and remove the leftovers manually, BUT...

After I regenerate the model (following additional changes in the schema etc.), I'm getting the "Company" association once again on the "User" object (along with the "CompanyId" property), which of course causes errors of mappings, since I'm having 2 mappings to the same CompanyId field in the database. Going once again to the xml to fix it is not something I'd like to do...

Is there a way around this? -Taking the "Company" table out to another model is not possible.

Thanks, Nir.

+1  A: 

I think I found the answer. I can leave the entity association without the scalar property, and set it to a private getter. Then, add to the partial class the following:

public int CompanyId {

get
  {
 return 
     (int)CompanyReference.EntityKey.EntityKeyValues.First(c => c.Key == "Id").Value; 
  }

}

That way I don't need to go to the database to fetch the company association along with the user, but I still have the value.

Nir.

nirpi

related questions