Avatar of DG_Dan
DG_Dan
 asked on

SQL Foreign Keys do not generate Entity Framework Associations

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

Visual Basic.NETASP.NETMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
DG_Dan

8/22/2022 - Mon
BuggyCoder

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...
DG_Dan

ASKER
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.
BuggyCoder

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
DG_Dan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
DG_Dan

ASKER
Decided to use a method that makes my original question obsolete