UpdateModelFromDatabaseException when trying to add a table to Entity Framework model
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.
The issue wound up being from trying to put the objects in an Entities
namespace. Apparently it's a reserved keyword that conflicts with the tools.
精彩评论