由于数据库丢弃在MVC中的代码优先方法,所有记录都被删除 [英] All Records got deleted because of Database Drop in code first approach in MVC

查看:93
本文介绍了由于数据库丢弃在MVC中的代码优先方法,所有记录都被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



因为所有EF根据我们在前端自动创建的类创建数据库,表和列,代码第一种方法。



因此,在创建数据库,表格和专栏后,我在课堂上再添加了一个额外的coloumn。



但是,我得到了以下异常。



EntityFramework.dll中出现System.InvalidOperationException类型的异常,但未在用户代码中处理



附加信息:自创建数据库以来,支持'MvcMusicStoreContext'上下文的模型已更改。考虑使用Code First Migrations来更新数据库(http://go.microsoft.com/fwlink/?



这是因为迁移。



所以,我在Global.asax.cs(Application_Start)中添加了以下行来修复该异常。



数据库。 SetInitializer(new DropCreateDatabaseIfModelChanges< employeedbcontext>());



现在工作正常。



但是问题我的现有数据库和表值被删除并创建new为new。我的意思是只有列名可用。



在MVC中表现不错?怎么能我解决了这个问题。

Hello,

as all EF creates database,table and column according our class created in front-end automatically in code first approach.

So, i added one more extra coloumn in my class after creating Data base ,table and column.

But, i got the following exception .

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The model backing the 'MvcMusicStoreContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?

this is because of Migration.

so, i added the follow line in Global.asax.cs(Application_Start) to fix that exception.

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<employeedbcontext>());

now works fine.

But the problem my existing database and table values got dropped and creates new as new with empty . i mean there is just column name only available.

is that good in MVC? how can i fix this problem.

推荐答案

你采取的路由是一个初始化策略,它的行为是以预期的方式。你不想用它来做一个生产数据库,您想要使用的是迁移。



Code First迁移详细描述于:

<一小时EF = http://msdn.microsoft.com/en-us/data/jj591621.aspx> http://msdn.microsoft.com/en-us/data/jj591621.aspx [<一个href =http://msdn.microsoft.com/en-us/data/jj591621.aspxtarget =_ blanktitle =新窗口> ^ ]



您也可以编写自己的初始化,但除非您想开始了解EF如何与数据库交互的杂草,否则我不会沿着这条路走下去。



这里最重要的一课:迁移!=初始化策略
The route that you took is an initialization strategy, and it is behaving in the expected manner. You don't want to use it for a production database, what you'll want to use instead is a Migration.

Code First migrations are described in detail at:
http://msdn.microsoft.com/en-us/data/jj591621.aspx[^]

You can also write your own initialize, but I would not go down that route unless you want to start getting into the weeds of how EF interacts with the database.

The most important lesson here: migrations != initialization strategy


这篇关于由于数据库丢弃在MVC中的代码优先方法,所有记录都被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆