使用 EF5.0 运行 MVC4 项目
在开发我的模型时,EF 会在每次模型更改时创建一个新数据库,并使用示例数据进行播种。模型设计现已完成,数据库中填充了实时数据,因此我对负责创建数据库的行进行了注释:
//System.Data.Entity.Database.SetInitializer(new SampleData());
但是在尝试编译时,我遇到了:
Cannot create xx because it already exists. Change the file path or the file name, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
我什至从项目中排除了带有 Seed 方法的文件,因此无法调用“DropCreateDatabaseIfModelChanges” - 据说。
最佳答案
您可以禁用默认初始化程序
System.Data.Entity.Database.SetInitializer<MyContextName>(null);
还有一种 web.config 方式:http://msdn.microsoft.com/en-US/data/jj556606
关于entity-framework - 未运行 SetInitializer 但 EF 仍在尝试 CreateDatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13493506/