Vincent
Vincent

Reputation: 1537

Create mysql database from entity classes

I have recently lost my mysql database, it was just a hobby project but i would like to recreate it. I do still have the entity model created in my .net project, this contains all the classes and connections it used to have. Is it possible to recreate my database from this in an easy way?

EDIT
I have tried generating the database from model by rightclicking in the library.edmx but i cannot execute it, it generates code for a ms SQL database.

Yes i did try it without the drop tables (so only using the code under "Creating all tables" but this syntax isn't correct) I am trying to rewrite it to work on mysql now.

The generated code: -- -------------------------------------------------- -- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure -- -------------------------------------------------- -- Date Created: 05/29/2015 22:34:12 -- Generated from EDMX file: 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
-- ------------------------------

Upvotes: 0

Views: 633

Answers (1)

KM11
KM11

Reputation: 757

Right-click an empty space on the Entity Designer surface and select Generate Database from Model

Source

EDIT: This will generate a SQL Server database. I don't think you can get MySQL syntax from your Visual Studio entity model.

EDIT2:

I have tried but i just cannot create it, i've tried running the code but i need to give a SQL server which i don't have

Try this:

  • View -> Server Explorer
  • Right click on Data Connections -> Add Connection…
  • If you haven’t connected to a database from Server Explorer before you’ll need to select Microsoft SQL Server as the data source
  • Connect to either LocalDb ((localdb)\v11.0) or SQL Express (.\SQLEXPRESS), depending on which one you have installed, and enter [dbo] as the database name
  • Select OK and you will be asked if you want to create a new database, select Yes
  • The new database will now appear in Server Explorer, right-click on it and select New Query
  • Copy your previously generated SQL into the new query, then right-click on the query and select Execute

Source (1st step)

Upvotes: 1

Related Questions