I have two objects, a Press and a Stitching:
Press
------------------
PressID
Name
Stitching
------------------
StitchingID
Name
Cost
So on my site, when a user creates a press, they have the option to create a Stitching for that press. A press doesn't have to have a stitching.
I'm using Entity Framework 4, and if I get a Press object from the database, I'd like to be able to say:
Press p = getPressFromDB(pressId);
if (p.Stitching != null)
{
float cost = p.Stitching.Cost;
}
And I'd also like to be able to do:
Stitching s = getStitchingFromDB(stitchingId);
Press p = s.Press;
How can I create a one-to-one relationship between these two tables?
EDIT:
I followed the advice of a user on another forum and ran this script to create a relationship between the two and prevent a Stitching from associating with multiple Presses:
CREATE TABLE PressStitching (PressID int NOT NULL PRIMARY KEY, StitchingID int NOT NULL,
CONSTRAINT FK_PressStitching_Press FOREIGN KEY(PressID) REFERENCES Press(PressID),
CONSTRAINT FK_PressStitching_Stitching FOREIGN KEY(StitchingID) REFERENCES Stitching(StitchingID),
CONSTRAINT UNQ_Stitching UNIQUE(StitchingID))
But when I update my .edmx file, I still get a one-to-many relationship. A Press has one Stitching, but a Stitching has multiple Press entities. I updated the relationship in the .edmx designer so that an Press had Zero-or-One of Stitching, and a Stitching had One of Equipment. But I don't know if that's enough. Any advice here?