【问题标题】:EF4 generates invalid scriptEF4 生成无效脚本
【发布时间】:2010-04-30 02:54:22
【问题描述】:

当我在.EDMX 文件中单击鼠标右键并单击Generate Database From Model 时,由于表名的原因,生成的脚本显然是错误的。它生成的是以下脚本。注意DROP TABLE 部分与CREATE TABLE 部分中的表名。

为什么不一致?

这显然不是一个可重用的脚本。我创建的是一个名为“地址”的实体和一个名为“公司”的实体等(都是单数)。 EntitySet 名称是复数形式。 “Pluralize New Objects”布尔值也不会改变这一点。那么有什么关系呢?

对于它的价值,我最初通过将 EDMX 指向一个具有非复数名称的表的数据库来生成 EDMX,现在我已经进行了一些更改,我想以另一种方式返回。我希望可以选择来回切换,因为 db-first 和 model-first 模型在所有情况下都不是理想的,并且我可以控制以确保不会出现多人同时使用的合并问题方式同时进行。

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- NOTE: if the constraint does not exist, an ignorable error will be reported.
-- --------------------------------------------------

    ALTER TABLE [Address] DROP CONSTRAINT [FK_Address_StateID-State_ID];
GO
    ALTER TABLE [Company] DROP CONSTRAINT [FK_Company_AddressID-Address_ID];
GO
    ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_BossEmployeeID-Employee_ID];
GO
    ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_CompanyID-Company_ID];
GO
    ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_PersonID-Person_ID];
GO
    ALTER TABLE [Person] DROP CONSTRAINT [FK_Person_AddressID-Address_ID];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- NOTE: if the table does not exist, an ignorable error will be reported.
-- --------------------------------------------------

    DROP TABLE [Address];
GO
    DROP TABLE [Company];
GO
    DROP TABLE [Employee];
GO
    DROP TABLE [Person];
GO
    DROP TABLE [State];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Addresses'
CREATE TABLE [Addresses] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [StreetAddress] nvarchar(100)  NOT NULL,
    [City] nvarchar(100)  NOT NULL,
    [StateID] int  NOT NULL,
    [Zip] nvarchar(10)  NOT NULL
);
GO

-- Creating table 'Companies'
CREATE TABLE [Companies] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(100)  NOT NULL,
    [AddressID] int  NOT NULL
);
GO

-- Creating table 'People'
CREATE TABLE [People] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [FirstName] nvarchar(100)  NOT NULL,
    [LastName] nvarchar(100)  NOT NULL,
    [AddressID] int  NOT NULL
);
GO

-- Creating table 'States'
CREATE TABLE [States] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(100)  NOT NULL,
    [Abbreviation] nvarchar(2)  NOT NULL
);
GO

-- Creating table 'Employees'
CREATE TABLE [Employees] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [PersonID] int  NOT NULL,
    [CompanyID] int  NOT NULL,
    [Position] nvarchar(100)  NOT NULL,
    [BossEmployeeID] int  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID] in table 'Addresses'
ALTER TABLE [Addresses]
ADD CONSTRAINT [PK_Addresses]
    PRIMARY KEY ([ID] );
GO

-- Creating primary key on [ID] in table 'Companies'
ALTER TABLE [Companies]
ADD CONSTRAINT [PK_Companies]
    PRIMARY KEY ([ID] );
GO

-- Creating primary key on [ID] in table 'People'
ALTER TABLE [People]
ADD CONSTRAINT [PK_People]
    PRIMARY KEY ([ID] );
GO

-- Creating primary key on [ID] in table 'States'
ALTER TABLE [States]
ADD CONSTRAINT [PK_States]
    PRIMARY KEY ([ID] );
GO

-- Creating primary key on [ID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [PK_Employees]
    PRIMARY KEY ([ID] );
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [StateID] in table 'Addresses'
ALTER TABLE [Addresses]
ADD CONSTRAINT [FK_Address_StateID_State_ID]
    FOREIGN KEY ([StateID])
    REFERENCES [States]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Address_StateID_State_ID'
CREATE INDEX [IX_FK_Address_StateID_State_ID]
ON [Addresses]
    ([StateID]);
GO

-- Creating foreign key on [AddressID] in table 'Companies'
ALTER TABLE [Companies]
ADD CONSTRAINT [FK_Company_AddressID_Address_ID]
    FOREIGN KEY ([AddressID])
    REFERENCES [Addresses]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Company_AddressID_Address_ID'
CREATE INDEX [IX_FK_Company_AddressID_Address_ID]
ON [Companies]
    ([AddressID]);
GO

-- Creating foreign key on [AddressID] in table 'People'
ALTER TABLE [People]
ADD CONSTRAINT [FK_Person_AddressID_Address_ID]
    FOREIGN KEY ([AddressID])
    REFERENCES [Addresses]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Person_AddressID_Address_ID'
CREATE INDEX [IX_FK_Person_AddressID_Address_ID]
ON [People]
    ([AddressID]);
GO

-- Creating foreign key on [CompanyID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_CompanyID_Company_ID]
    FOREIGN KEY ([CompanyID])
    REFERENCES [Companies]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_CompanyID_Company_ID'
CREATE INDEX [IX_FK_Employee_CompanyID_Company_ID]
ON [Employees]
    ([CompanyID]);
GO

-- Creating foreign key on [BossEmployeeID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_BossEmployeeID_Employee_ID]
    FOREIGN KEY ([BossEmployeeID])
    REFERENCES [Employees]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_BossEmployeeID_Employee_ID'
CREATE INDEX [IX_FK_Employee_BossEmployeeID_Employee_ID]
ON [Employees]
    ([BossEmployeeID]);
GO

-- Creating foreign key on [PersonID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_PersonID_Person_ID]
    FOREIGN KEY ([PersonID])
    REFERENCES [People]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_PersonID_Person_ID'
CREATE INDEX [IX_FK_Employee_PersonID_Person_ID]
ON [Employees]
    ([PersonID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------

【问题讨论】:

    标签: visual-studio-2010 entity-framework-4


    【解决方案1】:

    我一直无法找到这个问题的答案。此外,我在重新创建这个问题时遇到了问题,我不会从 DB-first 到 model-first 场景来回切换。因此,我只是假设来回导致问题,并且是让 EF 通过的糟糕情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2011-12-20
      • 2015-12-29
      • 2012-08-05
      • 1970-01-01
      相关资源
      最近更新 更多