Using Entity Designer in VS2010 to build a database for SQL Server 2005. Having a problem when I try to actually build the tables via the SQL generated by “generate database from model”; the tables are all created correctly, but it croaks on the foreign key constraints.
The entities at issue are laid out here:
World:
WORLD_ID [int32] (Primary Key)
Name [String]
Zone:
WORLD_ID [int32] (Primary key, FK on World.WORLD_ID)
ZONE_ID [int32] (Primary key)
Name [String]
Region:
WORLD_ID [int32] (Primary key, FK on Zone.WORLD_ID)
ZONE_ID [int32] (Primary key, FK on Zone.ZONE_ID)
REGION_ID [int32] (Primary key)
Name [String]
This part of the generated SQL code is the problem:
-- Creating foreign key on [ZONE_ID], [WORLD_ID] in table 'Regions'
ALTER TABLE [dbo].[Regions]
ADD CONSTRAINT [FK_ZoneRegion]
FOREIGN KEY ([ZONE_ID], [WORLD_ID])
REFERENCES [dbo].[Zones]
([ZONE_ID], [WORLD_ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
The error is this:
There are no primary or candidate keys in the referenced table ‘dbo.Zone’
that match the referencing column list in the foreign key ‘FK_ZoneRegion’.
I have to assume the issue has something to do with the fact that, in my multi-column primary key (WORLD_ID+ZONE_ID+REGION_ID), both ZONE_ID and WORLD_ID are foreign keys, and specifically that WORLD_ID is actually also a foreign key in the table with the source for the FK.
Is there something wrong with my structure here? Is it not OK for me to just have a single association going from Zone -> Region, and in fact I have to have two associations, one from World -> Region and one from Zone -> Region?
Thanks,
-Dan
For what it’s worth, I ended up just re-building the entity diagram from scratch, and it worked. No idea whether I had an error in my initial diagram, or whether this was a glitch with Entity Designer.