实体框架DB首先不添加表

时间:2015-09-11 04:36:43

标签: entity-framework

我在数据库中有下表,它使用复合主键,并且还有其他表的外键。我们正在从LINQ2SQL切换到实体框架,但由于某种原因,这个表不会被拉入。两列都不是空的,所以我认为它应该是一个有效的主键。

以下是创建表时表定义的内容。

CREATE TABLE [dbo].[WH_Subscriber]
(
    [EmployeeId] [int] NOT NULL,
    [WaLoId] [int] NOT NULL,

    PRIMARY KEY CLUSTERED ([EmployeeId] ASC, [WaLoId] ASC)
            WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                  IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                  ALLOW_PAGE_LOCKS = ON)
)
GO

ALTER TABLE [dbo].[WH_Subscriber] WITH CHECK 
      ADD FOREIGN KEY([EmployeeId])
      REFERENCES [dbo].[WH_Employee] ([EmployeeId])
GO

ALTER TABLE [dbo].[WH_Subscriber] WITH CHECK 
      ADD FOREIGN KEY([WaLoId])
      REFERENCES [dbo].[WH_WaLo] ([WaLoId])
GO

1 个答案:

答案 0 :(得分:0)

似乎复合主键无效,但我不能100%确定原因。但是我重新创建了表并添加了一个Identity列并应用了主键并显示了它。以下示例

    CREATE TABLE [dbo].[WH_Subscriber](
    [SubsciberId] [int] IDENTITY(1,1) NOT NULL,
    [EmployeeId] [int] NOT NULL,
    [WaLoId] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [SubsciberId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
 CONSTRAINT [uq_WH_Subscriber] UNIQUE NONCLUSTERED 
(
    [EmployeeId] ASC,
    [WaLoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO