在4.5框架下使用codeFirst添加和修改数据库文件小结。
建立:
1、建立模型类
2、InPackage Manager Console, run the command:
enable-migrations -ContextTypeName ContosoUniversityModelBinding.Models.SchoolContext
3、系统自动产生configuration.cs文件,其中的seed方法用来初始化数据库
4、Run the commandupdate-database
5、The database file has been added
修改:
1、修改模型类的字段
2、In the Package Management window, add a migration by running the commandadd-migration AddEnrollmentDate. Notice that the migration code adds the new Datetime column to the Student table.
3、Therefore, openConfiguration.csin the Migrations folder and remove or comment out the code in theSeedmethod. Save and close the file.
4、Now, run the commandupdate-database.