Solved

SQL Foreign Keys do not generate Entity Framework Associations

Posted on 2012-03-16
5
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach 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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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