views:

15

answers:

1

I have two tables, Customer and CustomerConfiguration that should have a one-to-one mapping. What I did was make customerId the primary key (and identity) for Customer, and then created a customerId field in CustomerConfiguration that is unique and is mapped to the Customer.customerId.

So this seems to give me what I need, and EF4 picks up on this okay (Im using RIA as well) in that it sees Customer to CustomerConfiguration as a 1 - 0..1 mapping.

But, never having done this, is this the right way to do this? Are there any gotchas that I need to watch out for?

+1  A: 

Sounds fine to me.

Customer table should have CustomerID PK, and other fields.

CustomerConfiguration table should have CustomerConfigurationID and CustomerID PK/FK.

Only point i'd make is remove the FK from the "CustomerConfiguration" entity (EF will put this in if you said "Include Foreign Keys in Model").

You should be able to get the CustomerConfiguration by doing this:

var someCust = ctx.Customers.SingleOrDefault(c => c.Id = 1); // get a cust
var someCustConfig = ctx.Customers.CustomerConfiguration;

That will use the "Navigational Property".

Other point i'd make it decide on lazy loading.

By default it's on, which means the above code will cause 2 database hits.

If you turn it off, and do this:

var someCust = ctx.Customers.Include("CustomerConfiguration").SingleOrDefault(c => c.Id = 1); // get a cust
var someCustConfig = ctx.Customers.CustomerConfiguration;

It will result in only 1 database hit, as it will 'eager-load' the relationship via an inner join.

RPM1984