2

我正在使用ASP.NET MVC 3,並通過MVC音樂商店和其他來源瞭解到。我能夠創建一個數據庫並將它生存下來,但奇怪的事情發生後大約5個小時,它出現故障並出現以下錯誤和堆棧跟蹤:EF4試圖重新創建數據庫,即使它已經在那裏?

注意:這似乎也是這種情況,只要我發佈該網站。我必須手動刪除數據庫以重新創建它,然後才能正常工作。但現在,我活着,我不能這樣做,因爲數據已經存儲在那裏,我不想重做。

File already exists. Try using a different database name. [ File name = C:\HostingSpaces\...\wwwroot\App_Data\Test.sdf ] 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlServerCe.SqlCeException: File already exists. Try using a different database name. [ File name = C:\HostingSpaces\mtnp55\pricexperience.com\wwwroot\App_Data\Test.sdf ] 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[SqlCeException (0x80004005): File already exists. Try using a different database name. [ File name = C:\HostingSpaces\...\wwwroot\App_Data\Test.sdf ]] 
    System.Data.SqlServerCe.SqlCeEngine.ProcessResults(IntPtr pError, Int32 hr) +72 
    System.Data.SqlServerCe.SqlCeEngine.CreateDatabase() +658 
    System.Data.SqlServerCe.SqlCeProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 timeOut, StoreItemCollection storeItemCollection) +194 
    System.Data.Objects.ObjectContext.CreateDatabase() +84 
    System.Data.Entity.Internal.DatabaseOperations.Create(ObjectContext objectContext) +8 
    System.Data.Entity.Database.Create() +76 
    System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +387 
    System.Data.Entity.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c) +75 
    System.Data.Entity.Internal.<>c__DisplayClass5.<PerformDatabaseInitialization>b__3() +19 
    System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +72 
    System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +169 
    System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +7 
    System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +118 
    System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +190 
    System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +73 
    System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +28 
    System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +62 
    System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15 
    System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +37 
    System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +63 
    PriceXperience.Controllers.AppsController.ViewLeaderboards(Int32 gameId) in C:\Users\Public\Documents\Development\...\Controllers\AppsController.cs:42 
    lambda_method(Closure , ControllerBase , Object[]) +101 
    System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17 
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27 
    System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263 
    System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191 
    System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 
    System.Web.Mvc.Controller.ExecuteCore() +116 
    System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 
    System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 
    System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37 
    System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 
    System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 
    System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50 
    System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7 
    System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 

我已經嘗試了很多東西,但仍然不知道它爲什麼這樣做。我有一個數據庫初始化。

System.Data.Entity.Database.SetInitializer<MobileEntities>(new SampleData()); 

public class SampleData : CreateDatabaseIfNotExists<MobileEntities> 

,當我嘗試訪問該數據庫運行的代碼如下:

public ActionResult ViewLeaderboards(int gameId) 
     { 
      var leaderboards = from u in mobileDB.Scores 
           where u.GameId == gameId 
           select u; 

      return View(leaderboards.OrderByDescending(u => u.Score).ToList()); 
     } 
+0

您應該能夠將您的數據庫初始值設定項保留爲CreateDatabaseIfNotExists,這是我的生產服務器上的功能,並且工作正常。 – Daveo

+0

這就是我的想法,但由於某種原因,我會在幾個小時後收到錯誤信息。我嘗試了傑克說的話,並且沒有錯誤,這是一個美好的一天。這是設置爲空。 – Tom

回答

2

當發佈你需要確保從Global.asax中移除初始化程序。初始化程序僅用於填充測試數據,不應在生產過程中使用。我還記得讀過將初始化器設置爲null的地方可能會起作用。

Database.SetInitializer<MyContext>(null); 

或者只是在發佈前調用SetInitializer的行註釋掉。在發佈之前,我也從項目中排除了我的Initializer類。

+0

如果我擺脫了初始值設定項,我仍然得到相同的錯誤,但將其設置爲null持續了一天,所以我們將在接下來的幾個小時內看到這種情況。 – Tom

0

Code First上下文的默認策略是CreateDatabaseIfNotExists的實例。 因此,有必要對包括

Database.SetInitializer<MyContext>(null); 

使默認初始化不叫那麼這可能會給出一個錯誤! 另外,將它設置爲null或編譯器會給出錯誤時,必須包含通用標記。

相關問題