How to declare one to one relationship using Entity Framework 4 Code First (POCO)

Three methods: A) Declare both classes with navigation properties to each other. Mark one of the tables (the dependent table) with the ForeignKey attribute on its Primary Key. EF infers 1-to-1 from this: public class AppUser { public int Id { get; set; } public string Username { get; set; } public OpenIdInfo OpenIdInfo { … Read more

Why use a 1-to-1 relationship in database design?

From the logical standpoint, a 1:1 relationship should always be merged into a single table. On the other hand, there may be physical considerations for such “vertical partitioning” or “row splitting”, especially if you know you’ll access some columns more frequently or in different pattern than the others, for example: You might want to cluster … Read more

JPA @OneToOne with Shared ID — Can I do this Better?

To map one-to-one association using shared primary keys use @PrimaryKeyJoinColumn and @MapsId annotation. Relevant sections of the Hibernate Reference Documentation: PrimaryKeyJoinColumn The PrimaryKeyJoinColumn annotation does say that the primary key of the entity is used as the foreign key value to the associated entity. MapsId The MapsId annotation ask Hibernate to copy the identifier from … Read more

Is there ever a time where using a database 1:1 relationship makes sense?

A 1:1 relationship typically indicates that you have partitioned a larger entity for some reason. Often it is because of performance reasons in the physical schema, but it can happen in the logic side as well if a large chunk of the data is expected to be “unknown” at the same time (in which case … Read more

One to one optional relationship using Entity Framework Fluent API

EF Code First supports 1:1 and 1:0..1 relationships. The latter is what you are looking for (“one to zero-or-one”). Your attempts at fluent are saying required on both ends in one case and optional on both ends in the other. What you need is optional on one end and required on the other. Here’s an … Read more

How can I make a JPA OneToOne relation lazy

First off, some clarifications to KLE‘s answer: Unconstrained (nullable) one-to-one association is the only one that can not be proxied without bytecode instrumentation. The reason for this is that owner entity MUST know whether association property should contain a proxy object or NULL and it can’t determine that by looking at its base table’s columns … Read more