Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Foreign Keys do not generate Entity Framework Associations

Posted on 2012-03-16
5
Medium Priority
?
513 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline

927 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