0
我在Visual Studio 2008中遇到了.NET 3.5 SP1中的實體框架出現的奇怪問題。我在SQL Server中創建了一個包含幾個表的數據庫,然後創建了關聯.edmx實體框架模型並且沒有問題。然後,我在數據庫中創建了一個新表,該表有一個到現有表的外鍵,並且需要添加到.edmx中。因此,我在Visual Studio中打開.edmx,並在右鍵單擊模型中選擇「從數據庫更新模型...」。我在「添加」選項卡中看到了新表格,因此我檢查了它並單擊完成。但是我得到以下文本的錯誤消息:UpdateModelFromDatabaseException嘗試向Entity Framework模型添加表時
---------------------------
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
---------------------------
僅供參考,這裏的表似乎是最相關的錯誤。 .edmx中已存在CustomerPreferences
。 Diets
是之後添加並嘗試添加到.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
這似乎是一個相當常見的用例,所以我不確定我要出錯的地方。