Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is EF4 trying to re-create my database even though the model hasn't changed?

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.
like image 854
Jedidja Avatar asked Oct 07 '10 14:10

Jedidja


People also ask

How do I use database first approach in Entity Framework?

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.


2 Answers

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!

like image 148
Steve Hobbs Avatar answered Oct 13 '22 09:10

Steve Hobbs


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

like image 25
Dave Avatar answered Oct 13 '22 11:10

Dave