Reputation: 16960
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?
Upvotes: 11
Views: 1774
Reputation: 11492
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
Upvotes: 0
Reputation: 3326
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!
Upvotes: 8
Reputation: 1847
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.
Upvotes: 1
Reputation: 2417
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
Upvotes: 3
Reputation: 985
Install and use SQL Server CE CTP1, which is still downloadable on Microsoft sites. This solved my problem with that.
Upvotes: 1