简体   繁体   English

如何在运行时使用Entity框架在sql数据库中创建表并在运行时更新模型?

[英]How to create tables in sql database with Entity framework at runtime and update model at runtime?

I know that when I use ado.net, I can create database or table at run-time programmatically like: 我知道当我使用ado.net时,我可以在运行时以编程方式创建数据库或表,如:

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. 任何帮助,将不胜感激。

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. 实体框架映射是静态的 - 动态修改需要,在运行时描述映射的XML文件的更改以及使用修改的文件。
  • 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". EF不支持dynamic因此唯一的方法是使用在创建表之前创建的新类动态加载程序集或大量使用Reflection.Emit并让程序“自行编写”。

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. 您可以使用该ADO.NET代码创建数据库和表,并仅将已修改的连接字符串传递给上下文的构造函数。 But the requirement is static database schema = no changes in table's definition. 但要求是静态数据库模式=表的定义没有变化。

Entity Framework also has similar functions like myCommand.ExecuteNonQuery(); 实体框架也有类似的功能,如myCommand.ExecuteNonQuery(); , you may try SomeEntities.ExecuteStoreCommand("some sql statements") . ,您可以尝试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. 因此,由于没有支持这种观点的可靠引用,我认为SomeEntities.ExecuteStoreCommand()可能是唯一的方法。

Please read this thread: NHibernate / Fluent NHibernate Dynamic Column Mapping 请阅读此主题: NHibernate / Fluent NHibernate动态列映射

better off using a key-value store such as Redis or a schema-less database like CouchDB instead of SQL 最好使用像Redis这样的键值存储或像CouchDB这样的无模式数据库而不是SQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM