数据库中有3表Person、Student、Worker。代码如下:
Code
USE [test]
GO
/**//****** 对象: Table [dbo].[Person] 脚本日期: 08/30/2008 11:44:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Person](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Sex] [nvarchar](50) NULL,
[Role] [nvarchar](50) NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
(
[PersonID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
USE [test]
GO
/**//****** 对象: Table [dbo].[Student] 脚本日期: 08/30/2008 11:51:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student](
[StudentID] [int] IDENTITY(1,1) NOT NULL,
[PersonID_FK] [int] NOT NULL,
[ClassID_FK] [int] NOT NULL,
[Title] [nvarchar](50) NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[StudentID] 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].[Student] WITH CHECK ADD CONSTRAINT [FK_Student_Person] FOREIGN KEY([PersonID_FK])
REFERENCES [dbo].[Person] ([PersonID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_Person]
USE [test]
GO
/**//****** 对象: Table [dbo].[Worker] 脚本日期: 08/30/2008 11:51:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Worker](
[WorkerID] [int] IDENTITY(1,1) NOT NULL,
[PersonID_FK] [int] NOT NULL,
[Company] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Worker] PRIMARY KEY CLUSTERED
(
[WorkerID] 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].[Worker] WITH CHECK ADD CONSTRAINT [FK_Worker_Person] FOREIGN KEY([PersonID_FK])
REFERENCES [dbo].[Person] ([PersonID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Worker] CHECK CONSTRAINT [FK_Worker_Person]
现在在VS里建个ADO.NET Entity Data Model,就叫默认的Model1.edmx好了,操作如下:
1、建立Model1.edmx时,在数据库中只选择Person表,完成Model1.edmx
2、编译,通过
3、现在从数据库中更新,把Student和Worker都添加进来,这是会看到设计器自动把实体和关系都一并生成出来,还算蛮聪明的
4、编译,还是通过
5、Bug开始来了,把Student和Worker实体删除
6、编译,出错,我在中文版和英文版都试过(打正式sp1的,team system),错误如下:
Error 1 Error 3013: Problem in Mapping Fragment starting at line 144: Missing table mapping: Foreign key constraint 'FK_Student_Person' from table Student (PersonID_FK) to table Person (PersonID): no mapping specified for the table Student.
Error 2 Error 3013: Problem in Mapping Fragment starting at line 144: Missing table mapping: Foreign key constraint 'FK_Worker_Person' from table Worker (PersonID_FK) to table Person (PersonID): no mapping specified for the table Worker.
按照微软讲师王然在webcast的说法,EF有时没有自动把后台代码删除完整,当时那webcast演示也是出错所以终止的。但他说是因为他的是beta版的原因。但我这几天测试了N次,发现在sp1正式版也出现这情况……郁闷
大家也出现过这现象吗?
[Sex] [nvarchar](50) NULL,
[Role] [nvarchar](50) NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
(
[PersonID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
USE [test]
GO
/**//****** 对象: Table [dbo].[Student] 脚本日期: 08/30/2008 11:45:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student](
[StudentID] [int] IDENTITY(1,1) NOT NULL,
[PersonID_FK] [int] NOT NULL,
[ClassID_FK] [int] NOT NULL,
[Title] [nvarchar](50) NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[StudentID] 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].[Student] WITH CHECK ADD CONSTRAINT [FK_Student_Class] FOREIGN KEY([ClassID_FK])
REFERENCES [dbo].[Class] ([ClassID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_Class]
GO
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [FK_Student_Person] FOREIGN KEY([PersonID_FK])
REFERENCES [dbo].[Person] ([PersonID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_Person]
USE [test]
GO
/**//****** 对象: Table [dbo].[Worker] 脚本日期: 08/30/2008 11:46:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Worker](
[WorkerID] [int] IDENTITY(1,1) NOT NULL,
[PersonID_FK] [int] NOT NULL,
[Company] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Worker] PRIMARY KEY CLUSTERED
(
[WorkerID] 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].[Worker] WITH CHECK ADD CONSTRAINT [FK_Worker_Person] FOREIGN KEY([PersonID_FK])
REFERENCES [dbo].[Person] ([PersonID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Worker] CHECK CONSTRAINT [FK_Worker_Person]