views:

381

answers:

3

Hi,

I have a table structure like the following:

Companies           Addresses
*********           *********
ID                  ID
AddressID           ...
BillingAddressID    ...

AddressID and BillingAddressID are foreign keys which are present in the Addresses table. When I generate my model based on this table instead of getting what I would expect to get (the AddressID, BillingAddressID) in the company class. I get the following:

public Addresses Addresses { .. }
public global::System.Data.Objects.DataClasses.EntityReference<Addresses> AddressesReference { .. }
public Addresses Addresses1 { .. }
public global::System.Data.Objects.DataClasses.EntityReference<Addresses> Addresses1Reference { .. }

It seems to be replacing BillingAddress with Addresses1 (not quite sure why that's happening). Also this seems to be common wherever I have a foreign key i.e. instead of the ID I get Table then the TableReference.

I think I can see whats happening i.e. instead of giving me the ID alone, it will be doing a lookup and finding the actual record the ID refers to. However, I am not quite sure what the TableReference field is for....

Can explain this a little better for me?

Thanks in advance.

A: 

The foreign keys are replaced by a reference to the entity (collection) the foreign key points to.

So to add an address to a company you would do something like:

Address a = new Address();
// ... set variables for address here

currentCompany.Addresses = a;
// or, the other way round (for collections)
a.Companies.Add(currentCompany);
Colin
Why do I get 2 references then? Addresses/AddressesReference. Also why does it replace BillingAddressID with Addresses1/Addresses1Reference?
James
A: 

EF uses the table names as the reference point when it builds the model and this is why you see "Addresses" and Addresses1". You can open up the entity model in the GUI format and click on each of the associations. These can be renamed to whatever you like, just click on the reference, view the mapping, ensure it is the one that maps "BillingAddressID" to "BillingAddressID" and rename that reference to "BillingAddress".

Note the current "Addresses" reference may be the one mapping the "BillingAddressID" so you have to check both references.

It would probably be best to change the mapping for "AddressID" to be "Address" instead of "Addresses" if it is a one to one mapping as well.

Jay
Thanks Jay. What is the purpose with the AddressReference field then? Do I use this in anyway?
James
+2  A: 

Relationships are represented as objects in Entity Framework, in the same manner as entities. Even if you are not going to work a lot directly on them, relationship object are first class citizens in EF. EF kreates ObjectStateEntry objects for tracking changes on relationships, just like it does it for entities.

That is why there are two references. First one, AddressesReference is a reference to the relationship object, not the exact entity, and second one Addresses is actual entity.

Peter Chan (link), and Julia Lerman in her book Programming Entity Framework, 1st Edition, say that understanding how relationship works in EF is very important. Also they mention that this is first thing that is confusing developer when they start using EF.

Misha N.
That cleared it up for me thanks!
James
Ill second that "this is the first thing that is confusing...". Right now I cant get anything to validate, their UI for this is horrible!
Marc