0

I have two tables in a SQL Server 2012 Express database that are defined like this.

SampleTableOne ColumnName nvarchar(50) Primary Key Id int

SampleTableTwo Id int Primary Key ColumnName nvarchar(50)

When I add a foreign key using the following command

ALTER TABLE SampleTableOne
ADD FOREIGN KEY(Id) REFERENCES SampleTableTwo(Id)
ON DELETE CASCADE
ON UPDATE CASCADE
GO

However when I add the models into Entity Framework, it is not adding the foreign key. I have "Include Foreign Key Columns In The Model" selected. Is there a step I am missing?

EDIT:

I am aiming for a one to one relationship.

2
  • A suggestion: Name and number are SQL server reserved keyword. It s not a good to use as table column name. Your DBA would not like it Commented Feb 9, 2013 at 5:23
  • I just used the numbers as example names, Ill change it Commented Feb 9, 2013 at 6:20

1 Answer 1

1

in the third part and sixth part of Associations in EF Code First saw the limitations of shared primary key association and argued that this type of association is relatively rare and in many schemas, a one-to-one and to Many-valued association is represented with a foreign key field and a unique constraint.

Sign up to request clarification or add additional context in comments.

2 Comments

A many-valued entity association is by definition a collection of entity references. One-to-many associations are the most important kind of entity association that involves a collection. We go so far as to discourage the use of more exotic association styles when a simple bidirectional many-to-one/one-to-many will do the job. A many-to-many association may always be represented as two many-to-one associations to an intervening class. This model is usually more easily extensible, so we tend not to use many-to-many associations in applications
This is supposed to be a one-to-one, does the same still hold true?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.