UpdateModelFromDatabaseException when trying to add a table to Entity Framework model

Posted by Agent_9191 on Stack Overflow See other posts from Stack Overflow or by Agent_9191
Published on 2010-03-15T19:32:25Z Indexed on 2010/05/25 21:11 UTC
Read the original article Hit count: 170

I'm running into a weird issue with Entity Framework in .NET 3.5 SP1 within Visual Studio 2008. I created a database with a few tables in SQL Server and then created the associated .edmx Entity Framework model and had no issues. I then created a new table in the database that has a foreign key to an existing table and needed to be added to the .edmx. So I opened the .edmx in Visual Studio and in the models right-clicked and chose "Update Model From Database...". I saw the new table in the "add" tab, so I checked it and clicked finish. However I get an error message with the following text:

---------------------------
Microsoft Visual Studio
---------------------------
An exception of type 'Microsoft.Data.Entity.Design.Model.Commands.UpdateModelFromDatabaseException' occurred while attempting to update from the database. The exception message is: 'Cannot update from the database. Cannot resolve the Name Target for ScalarProperty 'ID <==> CustomerID'.'.
---------------------------
OK   
---------------------------

For reference, here's the tables seem to be the most pertinent to the error. CustomerPreferences already exists in the .edmx. Diets is the table that was added afterwards and trying to add to the .edmx.

CREATE TABLE [dbo].[CustomerPreferences](
    [ID] [uniqueidentifier] NOT NULL,
    [LastUpdatedTime] [datetime] NOT NULL,
    [LastUpdatedBy] [uniqueidentifier] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[Diets](
    [ID] [uniqueidentifier] NOT NULL,
    [CustomerID] [uniqueidentifier] NOT NULL,
    [Description] [nvarchar](50) NOT NULL,
    [LastUpdatedTime] [datetime] NOT NULL,
    [LastUpdatedBy] [uniqueidentifier] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [ID] 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 [dbo].[Diets]  WITH CHECK ADD  CONSTRAINT [FK_Diets_CustomerPreferences] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[CustomerPreferences] ([ID])
GO

ALTER TABLE [dbo].[Diets] CHECK CONSTRAINT [FK_Diets_CustomerPreferences]
GO

This seems like a fairly common use case, so I'm not sure where I'm going wrong.

© Stack Overflow or respective owner

Related posts about visual-studio-2008

Related posts about entity-framework