tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Getting database script from DbContext (Code First)

6 May 2011 1 mins Entity Framework

I was speaking at Gopas Teched few days ago and there was a good question from audience about how to get the SQL script the DbContext is using to create database.

I never thought about it as I always create database in ER tool as it provides more features (like triggers, stored procedures etc.). But I remembered I implemented this method in .NET provider for Firebird. So it has to be somewhere.

The method is called CreateDatabaseScript and it’s on ObjectContext. So it was easy to expose it directly from DbContext, because it has ObjectContext under the hood (you can access it via IObjectContextAdapter).

public static string CreateDatabaseScript(this DbContext context)
{
	return ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();
}

Hope the questioner will find this blog post.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.