11

I have an ASP.NET MVC 3 Beta website using SQL Server CE 4.0. With both ScottGu's NerdDinner example and my own code, I will sometimes get the following exception as soon as I try to access the database:

File already exists. Try using a different database name. 
[ File name = D:\Sourcecode\NerdDinner\NerdDinner\App_Data\NerdDinners.sdf ]

Line 17:         public ActionResult Index()
Line 18:         {
Line 19:             var dinners = from d in nerdDinners.Dinners
Line 20:                           where d.EventDate > DateTime.Now
Line 21:                           select d;

[SqlCeException (0x80004005): File already exists. Try using a different database name. [ File name = D:\Sourcecode\NerdDinner\NerdDinner\App_Data\NerdDinners.sdf ]]
   System.Data.SqlServerCe.SqlCeEngine.ProcessResults(IntPtr pError, Int32 hr) +92
   System.Data.SqlServerCe.SqlCeEngine.CreateDatabase() +1584
   System.Data.SqlServerCe.SqlCeProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 timeOut, StoreItemCollection storeItemCollection) +287
   System.Data.Objects.ObjectContext.CreateDatabase() +84
   System.Data.Entity.Internal.DatabaseOperations.Create(ObjectContext objectContext) +35
   System.Data.Entity.Infrastructure.Database.Create() +70
   System.Data.Entity.Infrastructure.CreateDatabaseOnlyIfNotExists`1.InitializeDatabase(TContext context) +360
   System.Data.Entity.Infrastructure.Database.Initialize() +272
   System.Data.Entity.Internal.InternalContext.Initialize() +90
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +34
   System.Data.Entity.Internal.Linq.EfInternalQuery`1.Initialize() +140
   System.Data.Entity.Internal.Linq.EfInternalQuery`1.get_Provider() +29
   System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +34
   System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +63
   NerdDinner.Controllers.HomeController.Index() in D:\Sourcecode\NerdDinner\NerdDinner\Controllers\HomeController.cs:19

I cannot figure out why this works sometimes with an existing .dbf file and other times it complains. I have even tried explicitly setting the default behaviour with

Database.SetInitializer(new CreateDatabaseOnlyIfNotExists<...>());

Has anyone else experienced this?

  • Restarting Cassini doesn't seem to make a difference.
  • Hitting refresh in IE after receiving this error will make the exact same page load properly.
Jedidja
  • 16,610
  • 17
  • 73
  • 112
  • From your stacktrace: `CreateDatabaseOnlyIfNotExists`. The EF is *trying* to do the right thing. But somehow the check to see if the DB exists failed. Look into that. – Craig Stuntz Oct 07 '10 at 17:41
  • Here's the weird thing. Loading the .NET framework symbols/code and debugging the code actually stops the problem from happening. Every time. I don't get it. – Jedidja Oct 07 '10 at 18:44

5 Answers5

8

Sorry to resurrect such an old question, but I was experiencing this today and have found a workaround which does not involve installing an older version of the CTP.

From The correct url for the blogpost is http://www.hanselman.com/blog/PDC10BuildingABlogWithMicrosoftUnnamedPackageOfWebLove.aspx (about half-way through the blog post)

*Go into your AppStart_SQLCEEntityFramework.cs file and the DefaultConnectionFactory line to this:*

Database.DefaultConnectionFactory = new SqlCeConnectionFactory( 
    "System.Data.SqlServerCe.4.0", 
    HostingEnvironment.MapPath("~/App_Data/"),"");

My particular database is placed in the App_Data folder but I'm assuming that if yours is not you should be able to change the path to suit and it will work.

Hope this helps!

gyurisc
  • 11,234
  • 16
  • 68
  • 102
Steve Hobbs
  • 3,296
  • 1
  • 22
  • 21
3

It looks like this is a recently discovered bug. MSDN Forum

The workaround is to re-install SQL Server CE 4.0 CTP 1 download

Dave
  • 2,409
  • 3
  • 22
  • 28
1

Install and use SQL Server CE CTP1, which is still downloadable on Microsoft sites. This solved my problem with that.

zigomir
  • 985
  • 2
  • 15
  • 30
1

I had the same problem today with released MVC3 and SQL Server CE 4.0 downloaded with NuGet and it was resolved by uncommenting the line:

    DbDatabase.SetInitializer(new CreateCeDatabaseIfNotExists<MyContext>());

And replacing MyContext with the context class that was inheriting from DBContext in the models folder.

Case
  • 1,848
  • 21
  • 32
0

The final SQL Compact Edition 4.0 is out and after installing it the issue is not happening anymore. Here is the announcement from the team blog:

Microsoft SQL Server Compact 4.0 is available for download

gyurisc
  • 11,234
  • 16
  • 68
  • 102