2

私はASP.NET MVC 3を使用しており、MVC Music Storeやその他のソースから学習しました。私はデータベースを作成してそれを生存させることができましたが、それが生きていて約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

DB初期化子を自分のプロダクションサーバー上にあるCreateDatabaseIfNotExistsに保つことができ、正常に動作するはずです。 – Daveo

+0

それは私が思ったことですが、何らかの理由で、私がしたときに、数時間後にエラーが出るでしょう。私はジャックが言ったことを試してみました。それはエラーなしで良い一日を続けました。それはnullに設定されています。 – Tom

答えて

2

公開するときは、Global.asaxのから初期化子を削除してくださいする必要があります。イニシャライザは、テストデータを入力するためのものであり、製造中は使用しないでください。 Initializerをnullに設定すると動作するかもしれないということをどこか読んで覚えています。

Database.SetInitializer<MyContext>(null); 

また、パブリッシュする前にSetInitializerを呼び出す行をコメントアウトしてください。パブリッシュする前に、自分のInitializerクラスをプロジェクトから除外します。

+0

イニシャライザを取り除くと、同じエラーが発生しますが、nullに設定すると1日続いたので、今後数時間でどのようになるかを見ていきます。 – Tom

0

コードファーストコンテキストのデフォルト戦略は、CreateDatabaseIfNotExistsのインスタンスです。 だから、デフォルトの初期化子は、エラーを与える可能性があると呼ばれないように

Database.SetInitializer<MyContext>(null); 

を含める必要があります! また、汎用タグをnullに設定する場合は、そのタグを含める必要があります。そうしないと、コンパイラによってエラーが発生します。

関連する問題