Error: A file activation error occurred. CREATE DATABASE failed.

A file activation error occurred. CREATE DATABASE failed.

With Entity Framework 5 I was trying to play around with the enhancements to code first and establish a database with the AttachDbFileName property in the connection string section in the AppConfig file.

addname=DatabaseNameproviderName=System.Data.SqlClientconnectionString=Data Source=(LocalDB)v11.0;AttachDbFileName=|DataDirectory|DatabaseName.mdf;Integrated Security=True;MultipleActiveResultSets=True />

And I was receiving this error message:

A file activation error occurred. The physical file name ‘DatabaseName.mdf’ may be incorrect. Diagnose and correct additional errors, and retry the operation.

CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

It turns out that the ‘|DataDirectory|‘ property being used in the connection string wasn’t evaluating as expected. From the net I understood that it should evaluate to application folder unless specifically set in code. So I resulted to setting it in code to get around the issue:

AppDomain.CurrentDomain.SetData(“DataDirectory”, System.IO.Directory.GetCurrentDirectory());

or

AppDomain.CurrentDomain.SetData(“DataDirectory”, Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));

Advertisements

3 thoughts on “Error: A file activation error occurred. CREATE DATABASE failed.

Comments are closed.