2

I know that when I use ado.net, I can create database or table at run-time programmatically like:

String str;
SqlConnection myConn = new SqlConnection("Server=localhost;Integrated security=SSPI;database=master");

str = "CREATE DATABASE MyDatabase ON PRIMARY " +
    "(NAME = MyDatabase_Data, " +
    "FILENAME = 'C:\\MyDatabaseData.mdf', " +
    "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
    "LOG ON (NAME = MyDatabase_Log, " +
    "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
    "SIZE = 1MB, " +
    "MAXSIZE = 5MB, " +
    "FILEGROWTH = 10%)";

SqlCommand myCommand = new SqlCommand(str, myConn);
try 
{
    myConn.Open();
    myCommand.ExecuteNonQuery();
    MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
    MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
    if (myConn.State == ConnectionState.Open)
    {
        myConn.Close();
    }
}

But I am using entity framework and I have some work in which I have to create tables at runtime. I have searched a lot about it but no way I got. Any help would be appreciated.

1

3 Answers 3

6
+50

Entity framework is not for scenarios where you don't have static database schema.

Reasons:

  • Entity framework mapping is static - dynamic modification requires, changes of XML files describing the mapping at runtime and using modified files instead.
  • Even with changed mapping you still don't have changed or newly created entity classes in your code. EF doesn't support dynamic so the only way is either dynamically load assembly with new classes created prior to creating tables or heavy usage of Reflection.Emit and let your program "write itself".

If you only need to create a database but tables will be always same you should be fine. You can use that ADO.NET code to create database and tables and pass only modified connection string to the constructor of your context. But the requirement is static database schema = no changes in table's definition.

1
  • Can you give an example on create a database but tables will be always same. And Also how to modify the connection string in app.config / web.config from the values of the variables.
    – Khushi
    Sep 29, 2013 at 17:49
2

Entity Framework also has similar functions like myCommand.ExecuteNonQuery();, you may try SomeEntities.ExecuteStoreCommand("some sql statements").

Considering Entity Framework requires mappings between data models and database, it looks not possible to create/modify tables directly with integrated query language (otherwise the data models need to be modified at run time). Therefore, with no solid references supporting this point of view, I think SomeEntities.ExecuteStoreCommand() may be the only way.

3
  • Then how can i use SomeEntities.AddTo..(); method for that table Jan 21, 2012 at 7:48
  • That's the problem. Because you don't have static data model mappings, SomeNonExistEntities.AddTo...() is not available when you write the code. The solution I can propose is to use SQL statements completely...
    – grapeot
    Jan 21, 2012 at 8:22
  • 1
    I admit it's not elegant at all. Considering .NET 4 has dynamic objects, there may be solutions related with dynamic in the future release of Entity Framework I guess? Or there may be more elegant solutions on current version? Let's wait for others' answers.
    – grapeot
    Jan 21, 2012 at 8:24
1

Please read this thread: NHibernate / Fluent NHibernate Dynamic Column Mapping

better off using a key-value store such as Redis or a schema-less database like CouchDB instead of SQL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.