简体   繁体   English

运行Entity Framework .cs文件以创建数据库表的命令

[英]Command to run the Entity Framework .cs files to create database tables

I have just pulled the solution of C# rest API. 我刚刚提出了C#rest API的解决方案。 In the solution under web project I have a Migration folder which contains a number of .cs files to create table in database. 在Web项目下的解决方案中,我有一个Migration文件夹,其中包含许多.cs文件以在数据库中创建表。

The files are like 84618186***_InititalCreate , 68686186***_AddXyz , 86454186***_ExtendXyz . 文件类似于84618186***_InititalCreate68686186***_AddXyz86454186***_ExtendXyz I have absolutely no knowledge about Entity Framework. 我完全不了解实体框架。

I tried the Add-Migration command but it added a file. 我尝试了Add-Migration命令,但添加了一个文件。 Update-database added some tables to the database but remaining tables are still not created. Update-database将一些表添加到数据库中,但其余表仍未创建。 What my little experience about this says is there must be a command which will create the tables in SQL Server from all these files. 我对此的一点经验是,必须有一个命令可以从所有这些文件在SQL Server中创建表。

So I just want to know how to do it. 所以我只想知道如何去做。 reference to any specific link on stack will also help I did searched for half an hour but didn't find anything helpful. 引用堆栈上的任何特定链接也可以帮助我搜索了半个小时,但没有发现任何有用的信息。

If you want to create a new database, change your connection string to point to the new, empty database. 如果要创建新数据库,请更改连接字符串以指向新的空数据库。 Then you can create an idempotent script that will walk through all the migrations and apply them: 然后,您可以创建一个幂等脚本,该脚本将遍历所有迁移并应用它们:

Update-Database -Script -SourceMigration: $InitialDatabase

https://msdn.microsoft.com/en-us/data/jj591621.aspx?f=255&MSPPError=-2147217396#idempotent https://msdn.microsoft.com/en-us/data/jj591621.aspx?f=255&MSPPError=-2147217396#idempotent

Another alternative would be to temporarily change your database initializer to one that recreates the database like DropCreateDatabaseAlways then switch it back. 另一种选择是暂时将数据库初始化程序更改为像DropCreateDatabaseAlways这样重新创建数据库的初始化程序,然后再将其切换回。

http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/ http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/

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

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