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?
Step 1 − Let's create a new console project with DatabaseFirstDemo name. Step 2 − To create the model, first right-click on your console project in solution explorer and select Add → New Items… Step 3 − Select ADO.NET Entity Data Model from middle pane and enter name DatabaseFirstModel in the Name field.
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With