Meta
Meta

Reputation: 489

Generating table creation SQL query out from a model class

Is there a way to generate a table creation SQL for a specific model class in entity framework?

Example, I have this:

public class Event
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public string Context { get; set; }
    public string EventMessage { get; set; }
    public string ModifiedBy { get; set; }
}

}

And expected is the SQL:

CREATE TABLE [dbo].[Event] (
[Id]           INT            IDENTITY (1, 1) NOT NULL,
[Context]      NVARCHAR (MAX) NULL,
[Date]         DATETIME2 (7)  NOT NULL,
[EventMessage] NVARCHAR (MAX) NULL,
[ModifiedBy]   NVARCHAR (MAX) NULL,
CONSTRAINT [PK_Event] PRIMARY KEY CLUSTERED ([Id] ASC)

);

Upvotes: 0

Views: 120

Answers (2)

Dave Ronson
Dave Ronson

Reputation: 148

What are you trying to achieve? You tagged with entity-framework-core so in EF Core 1.0 rc1 you can generate a SQL script based on your model classes using the command:

dnx ef migrations script

Upvotes: 0

TomTom
TomTom

Reputation: 62093

No.

The reason being that the C# model contains WAY LESS INFORMATION.

Specific data types, indices etc. - a lot less information.

Entity Framework does that by adding metadata in methods (where you can define for example a string to have a max length).

If you want to go this way - you either go very simplistic or need a way to manage this additional data for sql generation.

Upvotes: 4

Related Questions