更新时间:2021-08-09 19:45:43
我们拿“MusicStore”这个示例来说 ,我们可以先直接在Artists表中新增CreateTime字段,然后在项目的Artist类中也新增CreateTime字段:public virtual DateTime? CreateTime { get; set; }
这时如果我们运行程序,因为Artist类中的代码有改动,程序会出现如下错误:
--------------------------------------------------------------------------------
The model backing the 'MusicStoreDB' context has changed since the database was created.
Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example,
the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: 用户 'sa' 登录失败。
---------------------------------------------------------------------------------------
说数据上下文在数据库创建后发生了改变。
这时如果我们可以在Global的Application_Start()中取消数据上下文的检查,即将数据初始化设为Null,如:
则可实现同时修改表和类中字段,而数据不动。
学习交流群:364976091