Solved

SQL Foreign Keys do not generate Entity Framework Associations

Posted on 2012-03-16
5
500 Views
Last Modified: 2012-03-27
Whenever I add my tables from a database into my entity model editor, none of the foreign keys "auto-generate" the respective associations. I've included a few examples of table creation scripts in case they provide some insight into what I'm doing wrong (and to prove I'm actually using foreign/primary keys).

Here's what I've tried so far:

Checking/Unchecking Include foreign key columns in the model (in the Entity Data Model Wizard)

Checking/Unchecking Pluralize or singularize generated object names

Adding only two tables (the ones I posted below)
Getting rid of identity columns
I loaded the AdventureWorks database sample and imported it to the entity model, and those associations were generated. I compared my database settings/schema to it, and no differences jumped out at me.


I've used LINQ to SQL before with no issues. Those associations generate fine. But using LINQ to SQL is going to be a last resort for me. I'm honestly thinking of recreating the structure in the entity model and then using it to create the database, but that will take forever.

Thanks.

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [Games].[GameCategories](
	[GameCategoriesID] [int] IDENTITY(0,1) NOT NULL,
	[GAME_CATEGORY_ID] [int] NOT NULL,
	[GameCategoryDescription] [nvarchar](max) NULL,
	[GameCategoryProperty] [tinyint] NULL,
 CONSTRAINT [PK_GameCategories] PRIMARY KEY CLUSTERED 
(
	[GameCategoriesID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [Games].[GameTypes](
	[GameTypesID] [int] IDENTITY(0,1) NOT NULL,
	[GameCategoriesID] [int] NOT NULL,
	[GameTypeDescription] [nvarchar](50) NULL,
	[BARCODE_TYPE_ID] [smallint] NULL,
	[GameType_Table1ID] [int] NULL,
	[GameTypeFolder] [nvarchar](100) NULL,
	[StartOffset] [tinyint] NULL,
 CONSTRAINT [PK_GameTypes_1] PRIMARY KEY CLUSTERED 
(
	[GameTypesID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [Games].[GameTypes]  WITH CHECK ADD  CONSTRAINT [FK_GameTypes_GameCategoriesID] FOREIGN KEY([GameCategoriesID])
REFERENCES [Games].[GameCategories] ([GameCategoriesID])
GO

ALTER TABLE [Games].[GameTypes] CHECK CONSTRAINT [FK_GameTypes_GameCategoriesID]
GO

ALTER TABLE [Games].[GameTypes]  WITH CHECK ADD  CONSTRAINT [FK_GameTypes_Table1ID] FOREIGN KEY([GameType_Table1ID])
REFERENCES [Games].[Table1] ([Table1ID])
GO

ALTER TABLE [Games].[GameTypes] CHECK CONSTRAINT [FK_GameTypes_Table1ID]
GO

Open in new window

0
Comment
Question by:DG_Dan
  • 3
  • 2
5 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37732415
are the foriegn keys physically mapped in db or they are virtually mapped. Open the table (which contains foreign keys) in SQLServer management studio and go to the foriegn key column and check relationships...
0
 

Author Comment

by:DG_Dan
ID: 37733440
How do I check this? In SSMS, I right-clicked the table, and selected Design. Then I right-clicked the column and selected Relationships... I highlighted the foreign key name, but I don't see anything that indicates physical vs virtual mappings.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37734178
did you see the key icon in front of your foreign key in table. this can be checked by expanding columns in a table under ssms
0
 

Accepted Solution

by:
DG_Dan earned 0 total points
ID: 37753517
I ended up using a local database cache which doesn't carry over foreign keys anyway. So now I just set the foreign keys on the local database cache (after creating the sync class). Then when I import the database cache to the entity framework, the relationships are imported correctly.
0
 

Author Closing Comment

by:DG_Dan
ID: 37770407
Decided to use a method that makes my original question obsolete
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question