从实体类创建mysql数据库

时间:2015-05-29 20:24:44

标签: mysql .net entity-framework

我最近丢失了我的mysql数据库,这只是一个爱好项目,但我想重新创建它。我仍然在我的.net项目中创建了实体模型,它包含了它以前拥有的所有类和连接。是否可以通过简单的方式从中重新创建我的数据库?

编辑
我已经尝试通过右键单击library.edmx从模型生成数据库,但我无法执行它,它为ms SQL数据库生成代码。

是的,我确实没有删除表尝试它(所以只使用“创建所有表”下的代码,但这种语法不正确)我正在尝试将其重写为现在在mysql上工作

生成的代码:      - ------------------------------------------------ -      - 用于SQL Server 2005,2008和Azure的实体设计器DDL脚本      - ------------------------------------------------ -      - 创建日期:05/29/2015 22:34:12      - 从EDMX文件生成:E:\ projecten(school + eigen)\ Visual Studio \ Library \ Library \ Model \ Library.edmx      - ------------------------------------------------ -

SET QUOTED_IDENTIFIER OFF;
GO
USE [Library];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Book_ibfk_1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Book] DROP CONSTRAINT [FK_Book_ibfk_1];
GO
IF OBJECT_ID(N'[dbo].[FK_Book_ibfk_2]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Book] DROP CONSTRAINT [FK_Book_ibfk_2];
GO
IF OBJECT_ID(N'[dbo].[FK_Book_ibfk_3]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Book] DROP CONSTRAINT [FK_Book_ibfk_3];
GO
IF OBJECT_ID(N'[dbo].[FK_Lending_ibfk_1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Lending] DROP CONSTRAINT [FK_Lending_ibfk_1];
GO
IF OBJECT_ID(N'[dbo].[FK_Person_ibfk_1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Person] DROP CONSTRAINT [FK_Person_ibfk_1];
GO
IF OBJECT_ID(N'[dbo].[FK_Lending_ibfk_2]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Lending] DROP CONSTRAINT [FK_Lending_ibfk_2];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Author]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Author];
GO
IF OBJECT_ID(N'[dbo].[Book]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Book];
GO
IF OBJECT_ID(N'[dbo].[BookGenre]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BookGenre];
GO
IF OBJECT_ID(N'[dbo].[BookLanguage]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BookLanguage];
GO
IF OBJECT_ID(N'[dbo].[Country]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Country];
GO
IF OBJECT_ID(N'[dbo].[Lending]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Lending];
GO
IF OBJECT_ID(N'[dbo].[Person]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Person];
GO
IF OBJECT_ID(N'[dbo].[TableID]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TableID];
GO

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

-- Creating table 'Author'
CREATE TABLE [dbo].[Author] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [FirstName] varchar(50)  NULL,
    [Surname] varchar(50)  NULL,
    [Suffix] varchar(50)  NULL
);
GO

-- Creating table 'Book'
CREATE TABLE [dbo].[Book] (
    [ID] guid  NOT NULL,
    [Title] varchar(255)  NOT NULL,
    [PublicationDate] datetime  NULL,
    [Edition] varchar(50)  NULL,
    [Pages] int  NULL,
    [Isbn] varchar(50)  NULL,
    [Description] varchar(65535)  NULL,
    [Author_ID] int  NOT NULL,
    [BookGenre_ID] int  NULL,
    [BookLanguage_ID] int  NOT NULL
);
GO

-- Creating table 'BookGenre'
CREATE TABLE [dbo].[BookGenre] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Genre] varchar(50)  NOT NULL
);
GO

-- Creating table 'BookLanguage'
CREATE TABLE [dbo].[BookLanguage] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Language] varchar(50)  NOT NULL
);
GO

-- Creating table 'Country'
CREATE TABLE [dbo].[Country] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Country1] varchar(45)  NOT NULL,
    [Currency] varchar(3)  NULL,
    [Capital] varchar(30)  NULL
);
GO

-- Creating table 'Lending'
CREATE TABLE [dbo].[Lending] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [DateLend] datetime  NOT NULL,
    [DateReturn] datetime  NULL,
    [Book_ID] guid  NOT NULL,
    [Person_ID] int  NOT NULL
);
GO

-- Creating table 'Person'
CREATE TABLE [dbo].[Person] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [FirstName] varchar(50)  NOT NULL,
    [Suffix] varchar(50)  NULL,
    [Surname] varchar(50)  NOT NULL,
    [AdressLine1] varchar(100)  NOT NULL,
    [AdressLine2] varchar(100)  NULL,
    [City] varchar(50)  NOT NULL,
    [County] varchar(50)  NOT NULL,
    [Postcode] varchar(50)  NOT NULL,
    [PhoneNumber] varchar(15)  NOT NULL,
    [Country_ID] int  NOT NULL
);
GO

-- Creating table 'TableID'
CREATE TABLE [dbo].[TableID] (
    [ID] guid  NOT NULL,
    [Table] varchar(50)  NOT NULL
);
GO

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

-- Creating primary key on [ID] in table 'Author'
ALTER TABLE [dbo].[Author]
ADD CONSTRAINT [PK_Author]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Book'
ALTER TABLE [dbo].[Book]
ADD CONSTRAINT [PK_Book]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'BookGenre'
ALTER TABLE [dbo].[BookGenre]
ADD CONSTRAINT [PK_BookGenre]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'BookLanguage'
ALTER TABLE [dbo].[BookLanguage]
ADD CONSTRAINT [PK_BookLanguage]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Country'
ALTER TABLE [dbo].[Country]
ADD CONSTRAINT [PK_Country]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Lending'
ALTER TABLE [dbo].[Lending]
ADD CONSTRAINT [PK_Lending]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Person'
ALTER TABLE [dbo].[Person]
ADD CONSTRAINT [PK_Person]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'TableID'
ALTER TABLE [dbo].[TableID]
ADD CONSTRAINT [PK_TableID]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

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

-- Creating foreign key on [Author_ID] in table 'Book'
ALTER TABLE [dbo].[Book]
ADD CONSTRAINT [FK_Book_ibfk_1]
    FOREIGN KEY ([Author_ID])
    REFERENCES [dbo].[Author]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Book_ibfk_1'
CREATE INDEX [IX_FK_Book_ibfk_1]
ON [dbo].[Book]
    ([Author_ID]);
GO

-- Creating foreign key on [BookGenre_ID] in table 'Book'
ALTER TABLE [dbo].[Book]
ADD CONSTRAINT [FK_Book_ibfk_2]
    FOREIGN KEY ([BookGenre_ID])
    REFERENCES [dbo].[BookGenre]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Book_ibfk_2'
CREATE INDEX [IX_FK_Book_ibfk_2]
ON [dbo].[Book]
    ([BookGenre_ID]);
GO

-- Creating foreign key on [BookLanguage_ID] in table 'Book'
ALTER TABLE [dbo].[Book]
ADD CONSTRAINT [FK_Book_ibfk_3]
    FOREIGN KEY ([BookLanguage_ID])
    REFERENCES [dbo].[BookLanguage]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Book_ibfk_3'
CREATE INDEX [IX_FK_Book_ibfk_3]
ON [dbo].[Book]
    ([BookLanguage_ID]);
GO

-- Creating foreign key on [Book_ID] in table 'Lending'
ALTER TABLE [dbo].[Lending]
ADD CONSTRAINT [FK_Lending_ibfk_1]
    FOREIGN KEY ([Book_ID])
    REFERENCES [dbo].[Book]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Lending_ibfk_1'
CREATE INDEX [IX_FK_Lending_ibfk_1]
ON [dbo].[Lending]
    ([Book_ID]);
GO

-- Creating foreign key on [Country_ID] in table 'Person'
ALTER TABLE [dbo].[Person]
ADD CONSTRAINT [FK_Person_ibfk_1]
    FOREIGN KEY ([Country_ID])
    REFERENCES [dbo].[Country]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Person_ibfk_1'
CREATE INDEX [IX_FK_Person_ibfk_1]
ON [dbo].[Person]
    ([Country_ID]);
GO

-- Creating foreign key on [Person_ID] in table 'Lending'
ALTER TABLE [dbo].[Lending]
ADD CONSTRAINT [FK_Lending_ibfk_2]
    FOREIGN KEY ([Person_ID])
    REFERENCES [dbo].[Person]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Lending_ibfk_2'
CREATE INDEX [IX_FK_Lending_ibfk_2]
ON [dbo].[Lending]
    ([Person_ID]);
GO

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

1 个答案:

答案 0 :(得分:1)

右键单击实体设计器表面上的空白区域,然后选择Generate Database from Model

Source

编辑:这将生成一个SQL Server数据库。我不认为您可以从Visual Studio实体模型中获取MySQL语法。

EDIT2

  

我试过但是我无法创建它,我已经尝试过运行代码但是我需要提供一个我没有的SQL服务器

试试这个:

  • 查看 - >服务器资源管理器
  • 右键单击数据连接 - >添加连接...
  • 如果在需要选择Microsoft SQL Server作为数据源之前尚未从服务器资源管理器连接到数据库
  • 连接到LocalDb((localdb)\ v11.0)或SQL Express(。\ SQLEXPRESS),具体取决于您安装的是哪一个,并输入[dbo]作为数据库名称
  • 选择确定,系统将询问您是否要创建新数据库,选择是
  • 新数据库现在将显示在“服务器资源管理器”中,右键单击它并选择“新建查询”
  • 将以前生成的SQL复制到新查询中,然后右键单击查询并选择执行

Source (1st step)