我正在开发一个ASP.NET MVC 5应用程序。我在我的Models文件夹中为表创建了一个类,然后使用该类作为模型创建了一个控制器。当我这样做时,我没有想到的是,我正在建模的表是在除dbo之外的数据库模式中。我只是在我的网站到达我可以进入Index方法并发现它没有从表中呈现任何数据之后,我意识到我的错误。所以我进入了我在Models中创建的类,并将以下属性添加到表中:
[Table("Interview", Schema = "Prism")]
接下来我编译了它,VS很高兴作为一个蛤蜊。所以我跑了它,导航到控制器的索引,然后一切都变得沮丧。 VS给了我这个错误信息:
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=The model backing the 'PrismDeleteInterviewDB' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
Source=EntityFramework
StackTrace:
at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at PrismDeleteInterview.Controllers.InterviewsController.Index() in D:\Websites\PrismDeleteInterview\PrismDeleteInterview\Controllers\InterviewsController.cs:line 20
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
InnerException:
我最关心的是,我希望EF做的最后一件事就是消灭数据库。短语“考虑使用Code First Migrations来更新数据库......”是我关心的问题。更新数据库给我意味着它正在以某种方式更改数据库,可能会删除表等。这个数据库不仅仅是我正在处理的一个临时事情,而是我的其他同事。它存在并且具有真实数据。那么,这会消灭我的数据库吗?