# ef_codefirst_mysql_winapp **Repository Path**: stp111/ef_codefirst_mysql_winapp ## Basic Information - **Project Name**: ef_codefirst_mysql_winapp - **Description**: ef-codefirst winform - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-08-06 - **Last Updated**: 2022-08-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # EF CodeFirst数据库操作 使用EF CodeFirst定义模型,然后生成数据库。 请在Nuget Console下执行PS命令 ## 第一次创建数据库 ```shell >Enable-Migrations Checking if the context targets an existing database... The target context 'AIVOnline.TopologyModel.PathModel.AIVOnlineDbContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory. DbContext需要缺省的构造器! 定义一个,补上。 > Enable-Migrations Checking if the context targets an existing database... > Add-Migration 位于命令管道位置 1 的 cmdlet Add-Migration 请为以下参数提供值: Name: Mysql_Db_Init Scaffolding migration 'Mysql_Db_Init'. The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Mysql_Db_Init' again. Note: 下面执行Update-Database,就要创建数据库表了,如果已经存在表了,则会执行失败。 可以在执行之前,检查一下,如果有数据库表,先删除。 > Update-Database Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [202208050038339_Mysql_Db_Init]. Applying explicit migration: 202208050038339_Mysql_Db_Init. Running Seed method. ``` ## 更新数据库结构 更新了设计,变更了数据库中表的结果,这时要更新数据库。 ```shell > Add-Migration update_node_state Scaffolding migration 'update_node_state'. The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration update_node_state' again. > Update-Database; Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [202208050402211_update_node_state]. Applying explicit migration: 202208050402211_update_node_state. Running Seed method. ``` # 设置EF记录执行的SQL 在配置文件的EntitiFramework配置中,提交interceptor配置项 ```xml ``` # 定制EntityFramework的interceptor entityframework的包System.Data.Entity.Infrastructure.Interception定义了多种interceptor, 可以选择你需要实现的interceptor接口,实现它。 然后再运用的项目里配置上对应的interceptor。