【问题标题】:UpdateModelFromDatabaseException when trying to add a table to Entity Framework model尝试将表添加到实体框架模型时出现 UpdateModelFromDatabaseException
【发布时间】:2010-03-15 19:32:25
【问题描述】:

我在 Visual Studio 2008 中的 .NET 3.5 SP1 中遇到了一个奇怪的实体框架问题。我在 SQL Server 中创建了一个包含几个表的数据库,然后创建了关联的 .edmx 实体框架模型并且没有任何问题。然后,我在数据库中创建了一个新表,该表具有现有表的外键,需要添加到 .edmx 中。所以我在 Visual Studio 中打开了 .edmx,在模型中右键单击并选择“从数据库更新模型...”。我在“添加”选项卡中看到了新表,所以我检查了它并单击了完成。但是我收到一条带有以下文本的错误消息:

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

作为参考,这里的表格似乎与错误最相关。 CustomerPreferences 已存在于 .edmx 中。 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

这似乎是一个相当常见的用例,所以我不确定我哪里出错了。

【问题讨论】:

    标签: visual-studio-2008 entity-framework


    【解决方案1】:

    问题归结于试图将对象放在Entities 命名空间中。显然这是一个与工具冲突的保留关键字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多