Solved

SQL Foreign Keys do not generate Entity Framework Associations

Posted on 2012-03-16
5
488 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to calculate iops? 12 27
JavaScript error 1 33
File Upload Control on a ASP.NET Overlay Page 1 20
Hidden Field Value 10 34
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now